Javascript 删除嵌套数组中不匹配的对象
在嵌套数组中以通用方式使用拼接方法时遇到了问题。 因此,如果“filterArray”没有对象的引用,我想删除“bigArray”的“nestedArray”中的对象 我已经做了一个我想做的例子。 如果你们有关于我如何用原生javascript或Lodash改进它的解决方案,请随意输入。我真的卡住了 预期结果应为:Javascript 删除嵌套数组中不匹配的对象,javascript,arrays,json,nested,lodash,Javascript,Arrays,Json,Nested,Lodash,在嵌套数组中以通用方式使用拼接方法时遇到了问题。 因此,如果“filterArray”没有对象的引用,我想删除“bigArray”的“nestedArray”中的对象 我已经做了一个我想做的例子。 如果你们有关于我如何用原生javascript或Lodash改进它的解决方案,请随意输入。我真的卡住了 预期结果应为: sort.unsortedArray = { "bigArray" : [ {"id": 1, "text" : "This should be visible when d
sort.unsortedArray = { "bigArray" : [
{"id": 1, "text" : "This should be visible when done",
"nestedArray": [
{ "id": 2, "nestedText": "Sort me out!" }
]
},
{ "id": 2, "text" : "This should be visible when done",
"nestedArray": [
{ "id": 2, "nestedText": "This one should be visible in the coming array" }
]
}]}
这里有一个答案
var sort=this;
var init=函数(){
sort.outObjects();
};
sort.filterArray={“过滤器”:[
{“id”:3,“groupId”:1},
{id:2,“groupId:2}]};
sort.unsortedArray={“bigArray”:[
{“id”:1,“text”:“完成后应可见”,
“Nestedaray”:[
{“id”:1,“nestedText”:“整理这个!”,
{“id”:2,“nestedText”:“整理我吧!”,
{“id”:3,“nestedText”:“这个应该在即将到来的数组中可见”}]},
{“id”:2,“文本”:“完成后应可见”,
“Nestedaray”:[
{“id”:1,“nestedText”:“整理我吧!”,
{“id”:2,“nestedText”:“这个应该在即将到来的数组中可见”},
{“id”:3,“nestedText”:“把这个整理出来!”}]}
]},
sort.outObjects=函数(){
//检查对象是否在同一范围内
if(sort.filterray&&sort.unsortedArray){
//循环遍历“bigArray”对象
for(变量i=0;i
您可以生成一个对象,以便更快地访问和测试最终过滤
生成后,您可以迭代数据,检查是否需要更改并应用筛选
编辑:保留多个id的建议
var filterary={“filter”:[{“id”:3,“groupId”:1},{“id”:2,“groupId”:2}]},
unsortedArray={“bigArray”:[{“id”:1,“text”:“这应该在完成时可见”,“nestedArray”:[{“id”:1,“nestedText”:“排序这一个!”},{“id”:2,“nestedText”:“排序我!”},{“id”:3,“nestedText”:“这一个应该在即将到来的数组中可见”}},{“id”:2,“text”:“这应该在完成时可见”,“nestedArray”:[{“id”:1,“nestedText”:“Sort me out!”},{“id”:2,“nestedText”:“这个应该在即将到来的数组中可见”},{“id”:3,“nestedText”:“Sort This one!”}]},
filterObject=Object.create(空);
filterArray.filter.forEach(函数(a){
filterObject[a.groupId]=filterObject[a.groupId]| | Object.create(null);
filterObject[a.groupId][a.id]=true;
});
unsortedArray.bigArray.forEach(函数(a){
if(过滤器对象中的a.id){
a、 nestedArray=a.nestedArray.filter(函数(b){
返回filterObject[a.id][b.id];
});
}
});
console.log(filterObject);
log(unsortedArray);
这将是我的解决方案
var fa={“filter”:[{“id”:3,“groupId”:1},{“id”:2,“groupId”:2}]},
usa={“bigArray”:[{“id”:1,“文本”:“这应该在完成时可见”,“nestedArray”:[{“id”:1,“nestedText”:“排序这一个!”},{“id”:2,“nestedText”:“排序我!”},{“id”:3,“nestedText”:“这一个应该在即将到来的数组中可见”}]},{“id”:2,“文本”:“这应该在完成时可见”,“nestedArray”:[{“id”:1,”nestedText:“Sort me out!”},{“id”:2,“nestedText:“这个应该在即将到来的数组中可见”},{“id”:3,“nestedText:“Sort This one!”}]},
sa=fa.filter.reduce((p,c)=>{var f=p.find(f=>f.id==c.groupId);
f&(f.nestedArray=f.nestedArray.reduce((n,o)=>o.id==c.id?n.concat(o):n,[]);
返回p}
,美国;
console.log(sa);
如果“filterArray”没有对象的引用,这意味着什么“没有任何具有groupId
property@RomanPerekhrest确切地这就是我面临的问题。它只是对bigArray:s对象的引用。好的,显示应该如何显示预期的result@RomanPerekhrest好主意!我添加了预期的结果。谢谢。:)我试试看!好建议!:)@NinaScholz,你的结果和OP的预期结果不一样。考虑THOTER OPS结果与给定的数据不匹配?在结果和OP结果之间仔细地比较第一个<代码> NeStDAREX < /代码>。注意…@NinaScholz,是的!它在真正的解决方案上也起了作用!尼娜岩石!!!非常感谢你!:D
var sort = this;
var init = function(){
sort.outObjects();
};
sort.filterArray = { "filter" : [
{ "id": 3, "groupId": 1 },
{ "id": 2, "groupId": 2 }]};
sort.unsortedArray = { "bigArray" : [
{"id": 1, "text" : "This should be visible when done",
"nestedArray": [
{ "id": 1, "nestedText":"Sort this one out!" },
{ "id": 2, "nestedText": "Sort me out!" },
{ "id": 3, "nestedText": "This one should be visible in the coming array"}]},
{ "id": 2, "text" : "This should be visible when done",
"nestedArray": [
{ "id": 1, "nestedText": "Sort me out!" },
{ "id": 2, "nestedText": "This one should be visible in the coming array" },
{"id": 3, "nestedText": "Sort this one out!" }]}
]},
sort.outObjects = function (){
//Check that we got the objects in same scope
if(sort.filterArray && sort.unsortedArray){
//Loop through "bigArray" object
for( var i = 0; i< sort.unsortedArray.length; i++){
console.log("sort.unsortedArray.length : ", sort.unsortedArray.length);
//Loop through each "nestedArray":s objects
for( var j = 0; j< sort.unsortedArray[i].nestedArray.length; j++){
console.log("sort.unsortedArray[i].nestedArray.length : ", sort.unsortedArray[i].nestedArray.length);
//Loop through filterArray object and compare each object in nested array, if they dont match, delete them.
for( var k = 0; k< sort.filterArray.length; k++){
console.log("sort.filterArray.length : ", sort.filterArray.length);
if(sort.filterArray[k].id != sort.unsortedArray[i].nestedArray[j].id){
//Delete unmatching object from unsortedArray
sort.unsortedArray[i].nestedArray.splice(j,1);
console.log("sort.unsortedArray after splice : ", sort.unsortedArray);
}
}
}
}
}
else{
console.log("Missing connection to object",sort.filterArray, sort.unsortedArray);
}
}
init();