Javascript 过滤对象和嵌套对象的数组
我有一个如下所示的对象数组:Javascript 过滤对象和嵌套对象的数组,javascript,Javascript,我有一个如下所示的对象数组: var data = [ { type: "parent", children: [ { type: "parent", children: [ { type: "parent" }, { type: "parent", children: [
var data = [
{
type: "parent",
children: [
{
type: "parent",
children: [
{
type: "parent"
},
{
type: "parent",
children: [
{
type: "notParent"
},
{
type: "parent"
}
]
}
]
}
]
},
{
type: "parent",
children: [
{
type: "parent"
}
]
},
{
type: "parent",
children: [
{
type: "parent",
children: [
{
type: "parent"
},
{
type: "parent"
}
]
},
{
type: "notParent"
}
]
},
{
type: "notParent"
}
]
var data = [
{
type: "parent",
children: [
{
type: "parent",
children: [
{
type: "parent"
},
{
type: "parent",
children: [
{
type: "parent"
}
]
}
]
}
]
},
{
type: "parent",
children: [
{
type: "parent"
}
]
},
{
type: "parent",
children: [
{
type: "parent",
children: [
{
type: "parent"
},
{
type: "parent"
}
]
}
]
}
]
该数据是一个对象数组,可以将对象嵌套得很深。我要做的是创建一个新数组,其中只包含类型为“parent”的对象,并删除所有其他对象。我发现很难对嵌套很深的对象执行此操作。我尝试了以下多种方法:
var findAllParents = function(obj, type) {
if(obj.type === type) { return obj; }
for(var i in obj) {
if(obj.hasOwnProperty(i)){
var foundParents = findAllParents(obj[i], type);
if(foundParents) { return foundParents; }
}
}
return null;
};
我想让它返回如下数据:
var data = [
{
type: "parent",
children: [
{
type: "parent",
children: [
{
type: "parent"
},
{
type: "parent",
children: [
{
type: "notParent"
},
{
type: "parent"
}
]
}
]
}
]
},
{
type: "parent",
children: [
{
type: "parent"
}
]
},
{
type: "parent",
children: [
{
type: "parent",
children: [
{
type: "parent"
},
{
type: "parent"
}
]
},
{
type: "notParent"
}
]
},
{
type: "notParent"
}
]
var data = [
{
type: "parent",
children: [
{
type: "parent",
children: [
{
type: "parent"
},
{
type: "parent",
children: [
{
type: "parent"
}
]
}
]
}
]
},
{
type: "parent",
children: [
{
type: "parent"
}
]
},
{
type: "parent",
children: [
{
type: "parent",
children: [
{
type: "parent"
},
{
type: "parent"
}
]
}
]
}
]
仅返回类型为“parent”的所有对象,您可以复制
数据,然后应用递归函数,该函数将使用过滤器()从每个对象的子对象数组中删除所需的元素
var data=[{type:“parent”,children:[{type:“parent”},{type:“parent”}]},{type:“parent”},{type:“parent”},{type:“parent”},{type:“parent”},{type:“parent”},{type:“parent”},{type:“notParent”}]
功能移除(obj){
if(对象儿童){
obj.children=obj.children.filter(x=>x.type==“父项”);
obj.children.forEach(x=>remove(x))
}
}
让copy=JSON.parse(JSON.stringify(data));
copy.forEach(x=>remove(x));
控制台日志(副本)
const数据=[
{
键入:“父项”,
儿童:[
{
键入:“父项”,
儿童:[
{
类型:“父级”
},
{
键入:“父项”,
儿童:[
{
类型:“notParent”,
儿童:[
{
类型:“父级”
}
]
},
{
类型:“父级”
}
]
}
]
}
]
},
{
键入:“父项”,
儿童:[
{
类型:“父级”
}
]
},
{
键入:“父项”,
儿童:[
{
键入:“父项”,
儿童:[
{
键入:“父项”,
儿童:[
{
键入:“父项”,
儿童:[
{
类型:“notParent”
}
]
}
]
},
{
类型:“父级”
}
]
},
{
类型:“notParent”
}
]
},
{
类型:“notParent”
}
];
const filterCollectionItemsOfType=(集合,预期类型='parent')=>{
const filteredCollection=collection.filter((项)=>item.type==expectedType);
filteredCollection.forEach((项)=>{
if(item.children&&item.children.length){
item.children=filterCollectionItemsOfType(item.children,expectedType);
}
});
返回筛选集合;
}
const parentscolection=filterCollectionItemsOfType(数据);
console.log(parentscolection)代码>您希望它是一个1D的仍然嵌套的数组。请添加预期输出。执行此代码时有错误吗?@MaheerAli我已使用预期输出更新output@Variable没有错误,只是输出错误-非常感谢您的回答!很抱歉,我应该给出一个更好的例子,我已经用一个更准确的例子更新了我的问题,说明了我需要修改的数据。我需要检查的一些对象将被进一步嵌套,其中一些将处于顶层,因此obj.children.filter将无法工作。我已更正了代码片段,您可以再次尝试您的方案吗?非常感谢!我想知道是否有一种方法可以扩展您的答案,使其适用于更嵌套的数据您可以删除类型检查,只需检查(item.children)是否再次调用递归,只有当您想要获取特定类型的子项时,类型检查才有用。您能帮我找出输出和预期输出之间的差异吗?据我所见,它与repl.it/repls/naturalRewardingBusinessSoftware中的数据一起工作。我更新了代码中的数据,但这些数据不适用于深度嵌套的对象