[英]Remove element from array inside array of objects if present in another array of objects
I am working with a large array of objects. 我正在处理大量对象。 I have simplified my data structure to the following.
我将数据结构简化为以下内容。 Each object has an
id
and each id
has two arrays associated with it type1
and type2
. 每个对象都有一个
id
,每个id
具有两个与其关联的数组type1
和type2
。
const arr = [{id: "12345", type1: ["Hat 1", "Hat 3"], type2: ["Hat 2", "Glove 4"]},
{id: "12345", type1: ["Glove 1", "Hat 1"], type2: ["Glove 3", "Hat 2"]},
{id: "54321", type1: ["Jacket 1", "Hat 4"], type2: ["Hat 3", "Hat 4"]},
{id: "54321", type1: ["Glove 2", "Hat 2"], type2: ["Glove 3", "Jacket 4"]},
{id: "13579", type1: ["Hat 1", "Hat 2"], type2: ["Hat 3", "Hat 4"]},
{id: "13579", type1: ["Glove 1", "Glove 2"], type2: ["Glove 3", "Glove 4"]}]
I have a "lookup" array of objects. 我有一个“查找”对象数组。 Each object has an
id
and a title
每个对象都有一个
id
和一个title
const lookup = [{id: "12345", title: "Hat 1"},
{id: "12345", title: "Hat 2"},
{id: "12345", title: "Glove 3"},
{id: "54321", title: "Hat 3"}
{id: "54321", title: "Jacket 4"},
{id: "54321", title: "Glove 5"},
{id: "13579", title: "Hat 2"},
{id: "13579", title: "Jacket 3"}]
I need to use the "lookup" object for any matching id that has a title
I need to remove it from type1 or type2 or both. 我需要对具有
title
任何匹配ID使用“查找”对象,我需要将其从type1或type2或两者中删除。 So my resulting array of objects would look something like so 所以我得到的对象数组看起来像这样
const result = [{id: "12345", type1: ["Hat 3"], type2: ["Glove 4"]},
{id: "12345", type1: ["Glove 1"], type2: []},
{id: "54321", type1: ["Jacket 1", "Hat 4"], type2: ["Hat 4"]},
{id: "54321", type1: ["Glove 2", "Hat 2"], type2: ["Glove 3"]},
{id: "13579", type1: ["Hat 1"], type2: ["Hat 3", "Hat 4"]},
{id: "13579", type1: ["Glove 1", "Glove 2"], type2: ["Glove 3", "Glove 4"]}]
The duplicates and having to search through both arrays for any matching id's is confusing me. 重复项和必须在两个数组中搜索任何匹配的ID令我感到困惑。 Is there a simple way to do this or maybe a better way to structure the data so it's not so convoluted?
有没有简单的方法可以做到这一点,或者有更好的方法来结构化数据,使数据不会那么复杂?
Loop through arr
and for each entry of arr
, loop through lookup
to compare and modify arr
通过循环
arr
并为每个条目arr
,遍历lookup
,比较和修改arr
for(let arrEntry of arr) {
let id = arrEntry.id;
let type1 = arrEntry.type1;
let type2 = arrEntry.type2;
for(let lookupEntry of lookup) {
let title = lookupEntry.title;
if(lookupEntry.id === id && type1.includes(title)) {
type1.splice(type1.indexOf(title), 1);
}
if(lookupEntry.id === id && type2.includes(title)) {
type2.splice(type2.indexOf(title), 1);
}
}
}
console.log(arr)
Let's not mutate the original data, and create a new resultant array. 我们不要突变原始数据,而是创建一个新的结果数组。 you can use
map
, filter
and some
of Array
您可以使用
map
, filter
和some
Array
arr.map(({id, type1, type2}) => ({
id,
type1: type1.filter(t => !lookup.some(l => id===l.id && l.title === t)),
type2: type2.filter(t => !lookup.some(l => id===l.id && l.title === t))
}));
Here is an working example: 这是一个工作示例:
const arr =[ {id: "12345", type1: ["Hat 1", "Hat 3"], type2: ["Hat 2", "Glove 4"]}, {id: "12345", type1: ["Glove 1", "Hat 1"], type2: ["Glove 3", "Hat 2"]}, {id: "54321", type1: ["Jacket 1", "Hat 4"], type2: ["Hat 3", "Hat 4"]}, {id: "54321", type1: ["Glove 2", "Hat 2"], type2: ["Glove 3", "Jacket 4"]}, {id: "13579", type1: ["Hat 1", "Hat 2"], type2: ["Hat 3", "Hat 4"]}, {id: "13579", type1: ["Glove 1", "Glove 2"], type2: ["Glove 3", "Glove 4"]} ], lookup = [ {id: "12345", title: "Hat 1"}, {id: "12345", title: "Hat 2"}, {id: "12345", title: "Glove 3"}, {id: "54321", title: "Hat 3"}, {id: "54321", title: "Jacket 4"}, {id: "54321", title: "Glove 5"}, {id: "13579", title: "Hat 2"}, {id: "13579", title: "Jacket 3"} ], res = arr.map(({id, type1, type2}) => ({ id, type1: type1.filter(t => !lookup.some(l => id===l.id && l.title === t)), type2: type2.filter(t => !lookup.some(l => id===l.id && l.title === t)) })); console.log(res);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.