JSFiddle - React, Tailwind, and code Playground
JavaScript
const resultsReg = []
const resultsTimeout = []
const obj = {
key : {
foo: 'bar.jpg'
}
}
function findImages(object, results) {
for (var key in object) {
if (typeof object[key] === 'string') {
if (object[key].match(/\.(jpg)$/i) && !results.includes(object[key]) && results.length < 9) results.push(object[key]);
}
else if (typeof object[key] === 'object') {
findImages(object[key], results); // this works, but in some applications will give Maximum call stack size exceeded error
}
}
}
function findImagesTimeout(object, results) {
for (var key in object) {
if (typeof object[key] === 'string') {
if (object[key].match(/\.(jpg)$/i) && !results.includes(object[key]) && results.length < 9) results.push(object[key]);
}
else if (typeof object[key] === 'object') {
findImagesTimeout(object[key], results);
}
}
}
findImages(obj, resultsReg)
findImagesTimeout(obj, resultsTimeout)
console.log(resultsReg)
console.log(resultsTimeout)