Skip to content

Instantly share code, notes, and snippets.

@daniellizik
Last active May 17, 2016 03:10
Show Gist options
  • Save daniellizik/721c75c59180415da6c5dfac60051cfc to your computer and use it in GitHub Desktop.
Save daniellizik/721c75c59180415da6c5dfac60051cfc to your computer and use it in GitHub Desktop.
deep search key accumulate
function deepsearch_get_keys(haystack, bucket = []) {
let i = 0;
let p;
if (Object.prototype.toString.call(haystack) === '[object Array]') {
for (i; i < haystack.length; i++)
ds(haystack[i], bucket);
}
else if (Object.prototype.toString.call(haystack) === '[object Object]') {
for (p in haystack) {
bucket.push(p);
ds(haystack[p], bucket);
}
}
return bucket;
};
var fixture = {
a: [
{
a: 'a',
b: 'b',
ldkfj: 'flk',
fkj: 34545
},
{
fd: 454
}
],
b: 4546,
tklsd: 34546,
kg: 436754,
et: {
fjk: 325,
fkldj: 235,
joi: 65,
fksdjfl: [1, 2, 3, 5435, 6546],
fd: false
}
};
var result = deepsearch_get_keys(test);
console.log(result.length === 15, 'should have 15 keys');
console.log(result.indexOf('fd') > -1, 'should have fd key');
console.log(result.indexOf('bloohofhho') > -1, 'should not have random key');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment