如何在未知大小的重复嵌套数组中查找对象和父对象?使用lodash或本地javascript。
数组可能是这样的:
var modules = [{
name: 'Module1',
submodules: [{
name: 'Submodule1',
id: 1,
submodules: [{
name: 'Submodule11',
id: 1,
submodules: []
}, {
name: 'Submodule12',
id: 2,
submodules: [{
name: 'Submodule121',
id: 1,
submodules: []
}, {
name: 'Submodule122',
id: 2,
submodules: []
}]
}]
},
{
name: 'Submodule2',
id: 2,
submodules: []
}
]
},
{
name: 'Module2',
submodules: [{
name: 'Submodule1',
id: 3,
submodules: []
}, {
name: 'Submodule2',
id: 4,
submodules: []
}]
}
];
并且说数组中的所有“名称”属性都是唯一的。
我想找到:
name: 'Submodule122'
我正在使用此函数查找父对象,但它仅在数组的第一级起作用:
_.find(this.modules , function(item) {
return _.some(item.submodules, { name: 'Submodule122'});
这可以找到实际的对象,但它也仅适用于数组的第一级:
_(this.modules)
.thru(function (coll) {
return _.union(coll, _.map(coll, 'submodules'));
})
.flatten()
.find({ name: 'Submodule122'})
答案 0 :(得分:1)
您可以通过将实际对象保留为父对象来采取迭代和递归的方法。
function find(name, array, parent) {
var result;
array.some(object =>
object.name === name && (result = { object, parent }) ||
(result = find(name, object.submodules, object))
);
return result;
}
var modules = [{ name: 'Module1', submodules: [{ name: 'Submodule1', id: 1, submodules: [{ name: 'Submodule11', id: 1, submodules: [] }, { name: 'Submodule12', id: 2, submodules: [{ name: 'Submodule121', id: 1, submodules: [] }, { name: 'Submodule122', id: 2, submodules: [] }] }, { name: 'Submodule2', id: 2, submodules: [] }] }, { name: 'Module2', submodules: [{ name: 'Submodule1', id: 3, submodules: [] }, { name: 'Submodule2', id: 4, submodules: [] }] }] }];
console.log(find('Submodule122', modules));
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 1 :(得分:0)
var modules = [{
name: 'Module1',
submodules: [{
name: 'Submodule1',
id: 1,
submodules: [{
name: 'Submodule11',
id: 1,
submodules: []
}, {
name: 'Submodule12',
id: 2,
submodules: [{
name: 'Submodule121',
id: 1,
submodules: []
}, {
name: 'Submodule122',
id: 2,
submodules: []
}]
}]
},
{
name: 'Submodule2',
id: 2,
submodules: []
}
]
},
{
name: 'Module2',
submodules: [{
name: 'Submodule1',
id: 3,
submodules: []
}, {
name: 'Submodule2',
id: 4,
submodules: []
}]
}
];
var findByName = (name, module, parent, item) => {
parent.push(...module.filter(x => Array.isArray(x.submodules) && x.submodules.some(y => y.name == name)));
item.push(...module.filter(y => y.name == name));
module.forEach(x => {
if (Array.isArray(x.submodules) && x.submodules.length > 0) {
findByName(name, x.submodules, parent, item);
}
});
}
var parents = [], items = [];
findByName('Submodule12', modules, parents, items);
console.log(parents);
console.log(items);
答案 2 :(得分:0)
要求不完全相同,但我认为仍然值得分享。现在,我们将object-scan用于我们所有的数据处理任务,一旦您将其束之高阁,它就会非常强大。这是您回答问题的方式
请注意,这将返回所有父母,但是很容易选择想要的父母。
const objectScan = require('object-scan');
const search = (name, data) => objectScan(['**.name'], {
rtn: 'parents',
abort: true,
filterFn: ({ value }) => value === name
})(data);
const modules = [{"name":"Module1","submodules":[{"name":"Submodule1","id":1,"submodules":[{"name":"Submodule11","id":1,"submodules":[]},{"name":"Submodule12","id":2,"submodules":[{"name":"Submodule121","id":1,"submodules":[]},{"name":"Submodule122","id":2,"submodules":[]}]}]},{"name":"Submodule2","id":2,"submodules":[]}]},{"name":"Module2","submodules":[{"name":"Submodule1","id":3,"submodules":[]},{"name":"Submodule2","id":4,"submodules":[]}]}];
console.log(search('Submodule122', modules));
// => [ { name: 'Submodule122', id: 2, submodules: [] },
// [ { name: 'Submodule121', id: 1, submodules: [] },
// { name: 'Submodule122', id: 2, submodules: [] } ],
// { name: 'Submodule12',
// id: 2,
// submodules: [ [Object], [Object] ] },
// [ { name: 'Submodule11', id: 1, submodules: [] },
// { name: 'Submodule12', id: 2, submodules: [Array] } ],
// { name: 'Submodule1', id: 1, submodules: [ [Object], [Object] ] },
// [ { name: 'Submodule1', id: 1, submodules: [Array] },
// { name: 'Submodule2', id: 2, submodules: [] } ],
// { name: 'Module1', submodules: [ [Object], [Object] ] },
// [ { name: 'Module1', submodules: [Array] },
// { name: 'Module2', submodules: [Array] } ] ]
console.log(search('unknown', modules));
// => undefined