Let's say I have a deeply nested object or array, returned from an API, which may look like this:
const array = [
{
key1: 'dog',
key2: 'cat'
},
{
key1: 'dog',
key2: 'cat'
key3: [
{
keyA: 'mouse',
keyB: 'https://myURL.com/path/to/file/1'
keyC: [
{
keyA: 'tiger',
keyB: 'https://myURL.com/path/to/file/2'
}
]
}
]
},
{
key4: 'dog',
key5: 'https://myURL.com/path/to/file/3'
}
]
I want to traverse the object/array recursively and build an array of all the values that are strings and include the substring https://myURL.com, and building an array with the matches as follows:
let matches = [
'https://myURL.com/path/to/file/1',
'https://myURL.com/path/to/file/2',
'https://myURL.com/path/to/file/3'
]
An important note is that I DO NOT know in advance what the relevant key(s) are. A value that is a URL could be on any key, potentially. So, in this example I cannot simply look for keyB or key5 - I have to test every key:value pair for values that include a URL string.
Question
How do I create a recursive function to search the object for values that .includes() string 'https://myURL.com'? Happy to use a lodash method, so feel free to submit solutions that depend on lodash.
Context
The data comes from a CMS API and I will be using the array of URLS (matches above, in this example) to add assets to cache with Cache API, async in background, for offline functionality for a PWA.
Many thanks in advance.