Comparing version 1.6.1 to 1.6.2
{ | ||
"name": "deepdash", | ||
"version": "1.6.1", | ||
"version": "1.6.2", | ||
"description": "Object tree traversal for lodash", | ||
@@ -5,0 +5,0 @@ "main": "deepdash.js", |
@@ -119,3 +119,3 @@ <img src="deepdash.svg?sanitize=true" width="64px"/> | ||
obj, // The object to iterate over | ||
iteratee=_.identity, // The function invoked per iteration | ||
iteratee=_.identity, // The function invoked per iteration. Return `false` explicitly to skip children of current node. | ||
options={ | ||
@@ -140,3 +140,3 @@ track: false /* track parents from current back to the root, | ||
); | ||
return false; | ||
return false; // if `false` returned explicitly, children of current `value` will be skipped. | ||
} | ||
@@ -143,0 +143,0 @@ //do your things |
@@ -58,2 +58,92 @@ 'use strict'; | ||
}); | ||
//https://stackoverflow.com/questions/19483706/javascript-how-to-filter-deep-json-objects | ||
it('javascript-how-to-filter-deep-json-objects', () => { | ||
var obj = [ | ||
{ | ||
title: 'category 1', | ||
children: [ | ||
{ | ||
title: 'subcategory 11', | ||
children: [ | ||
{ id: 1, title: 'name 1' }, | ||
{ id: 2, title: 'name 2' }, | ||
{ id: 3, title: 'name 3' }, | ||
], | ||
}, | ||
{ title: 'subcategory 12', children: [{ id: 1, title: 'name 4' }] }, | ||
], | ||
}, | ||
{ | ||
title: 'category 2', | ||
children: [ | ||
{ | ||
title: 'subcategory 21', | ||
children: [ | ||
{ id: 3, title: 'name cat2sub1id3' }, | ||
{ id: 5, title: 'name cat2sub1id5' }, | ||
], | ||
}, | ||
{ | ||
title: 'subcategory 22', | ||
children: [ | ||
{ id: 6, title: 'name cat2sub2id6' }, | ||
{ id: 7, title: 'name cat2sub2id7' }, | ||
], | ||
}, | ||
], | ||
}, | ||
]; | ||
var idList = [2, 3]; | ||
// We will need 2 passes, first - to collect needed 'id' nodes: | ||
var foundIds = _.filterDeep( | ||
obj, | ||
function(value, key) { | ||
if (key == 'id' && _.indexOf(idList, value) !== -1) return true; | ||
}, | ||
// we need to disable condensing this time to keep all the paths in result object matching source, | ||
// otherwise array indexes may be changed and we will not find correct values in the source object later. | ||
{ condense: false } | ||
); | ||
// second pass - to put missed 'title' nodes both for found ids and their parents. | ||
var filtrate = _.filterDeep(obj, function( | ||
value, | ||
key, | ||
path, | ||
depth, | ||
parent, | ||
parentKey, | ||
parentPath | ||
) { | ||
if ( | ||
_.has(foundIds, path) || | ||
(key == 'title' && _.has(foundIds, parentPath)) | ||
) | ||
return true; | ||
}); | ||
expect(filtrate).to.deep.equal([ | ||
{ | ||
title: 'category 1', | ||
children: [ | ||
{ | ||
title: 'subcategory 11', | ||
children: [ | ||
{ title: 'name 1' }, | ||
{ id: 2, title: 'name 2' }, | ||
{ id: 3, title: 'name 3' }, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
title: 'category 2', | ||
children: [ | ||
{ | ||
title: 'subcategory 21', | ||
children: [{ id: 3, title: 'name cat2sub1id3' }], | ||
}, | ||
], | ||
}, | ||
]); | ||
// console.log(filtrate); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
56462
1455