[英]remove object property containing array inside array of objects if element 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 anywhere from 0 to 4 arrays associated with it. 每个对象都有一个
id
,每个id
都有0到4个与之关联的数组。 The array names are dynamically generated and will have at most 2 elements, and can be any number of names based on other requirements. 数组名称是动态生成的,最多包含2个元素,并且根据其他要求可以是任意数量的名称。 My initial object looks like this:
我的初始对象如下所示:
const arr = [{id: "12345", array1: ["Banana", "Apple"], array2: ["Orange", "Strawberry"]},
{id: "12345", array0: ["Potato", "Tomato"]},
{id: "54321", array0: ["Kiwi", "Apple"], array1: ["Potato", "Onion"]},
{id: "54321", array2: ["Orange", "Tomato"], array0: ["Kiwi", "Banana"]},
{id: "13579", array1: ["Banana", "Apple"], array2: ["Grapefruit", "Onion"]},
{id: "13579", array1: ["Potato", "Banana"], array2: ["Orange", "Pepper"]}]
I have a "lookup" array of objects. 我有一个“查找”对象数组。 Each object has an
id
and a type
每个对象都有一个
id
和一个type
const lookup = [{id: "12345", type: "Banana"},
{id: "12345", type: "Kiwi"},
{id: "12345", type: "Apple"},
{id: "54321", type: "Strawberry"}
{id: "54321", type: "Tomato"},
{id: "54321", type: "Banana"},
{id: "13579", type: "Tomato"},
{id: "13579", type: "Grapefruit"}]
I need to use the "lookup" object for any matching id that has a type
that is in any of the corresponding id's
arrays. 我需要对任何具有对应
id's
数组中任何type
匹配ID使用“查找”对象。 I need to remove that property from the object. 我需要从对象中删除该属性。 Lookups should be 1:1, So my resulting array of objects would look something like so
查找应该是1:1,所以我得到的对象数组看起来像这样
const result = [{id: "12345", array2: ["Orange", "Strawberry"]},
{id: "12345", array0: ["Potato", "Tomato"]},
{id: "54321", array0: ["Kiwi", "Apple"], array1: ["Potato", "Onion"]},
{id: "54321"},
{id: "13579", array1: ["Banana", "Apple"]},
{id: "13579", array1: ["Potato", "Banana"], array2: ["Orange", "Pepper"]}]
The part I am getting hung up on is not knowing the object key name ahead of time, and also how to search the object entries for that key with the lookup
object's type property. 我要挂断的部分是不提前知道对象键的名称,也不知道如何使用
lookup
对象的type属性在对象条目中搜索该键。 My initial thought was to use Object.values
but then I am not sure how to remove the object property if I use that. 我最初的想法是使用
Object.values
但是如果使用该属性,我不确定如何删除对象属性。
For each object in the lookup
check if the id matches in the arr
array. 对于
lookup
中的每个对象,请检查id是否与arr
数组匹配。 If matches then get all the keys from that specific object in arr
array using Object.keys. 如果匹配,则使用Object.keys从
arr
数组中的特定对象获取所有键。
pseudo code 伪码
arr
array. arr
数组中获取该对象。 Use Object.keys to get array of keys For example the array will be now 使用Object.keys获取键数组例如,该数组现在是
['id','array0','array1'] [ 'ID', 'ARRAY0', 'ARRAY1']
Now iterate this array and & use the element from this array as key name & check if the value is an array again. 现在,迭代此数组,并&将该数组中的元素用作键名,并再次检查该值是否为数组。 For example
例如
`{id: "12345", array1: ["Banana", "Apple"], array2: ["Orange", "Strawberry"] }['array0']`
will produce ["Banana", "Apple"]
. 将产生
["Banana", "Apple"]
。 In this check if the type exist using indexOf
, If not then use delete
to delete the key and value from the object 在这种情况下,使用
indexOf
检查类型是否存在,如果不存在,则使用delete
从对象中删除键和值
const arr = [{ id: "12345", array1: ["Banana", "Apple"], array2: ["Orange", "Strawberry"] }, { id: "12345", array0: ["Potato", "Tomato"] }, { id: "54321", array0: ["Kiwi", "Apple"], array1: ["Potato", "Onion"] }, { id: "54321", array2: ["Orange", "Tomato"], array0: ["Kiwi", "Banana"] }, { id: "13579", array1: ["Banana", "Apple"], array2: ["Grapefruit", "Onion"] }, { id: "13579", array1: ["Potato", "Banana"], array2: ["Orange", "Pepper"] } ] const lookup = [{ id: "12345", type: "Banana" }, { id: "12345", type: "Kiwi" }, { id: "12345", type: "Apple" }, { id: "54321", type: "Strawberry" }, { id: "54321", type: "Tomato" }, { id: "54321", type: "Banana" }, { id: "13579", type: "Tomato" }, { id: "13579", type: "Grapefruit" } ] let newArray = []; lookup.forEach(function(item, index) { arr.forEach(function(arrId, index1) { if (item.id === arrId.id) { Object.keys(arrId).forEach(function(elem) { if (Array.isArray(arrId[elem]) && arrId[elem].indexOf(item.type) !== -1) { delete arr[index1][elem] } }) } }) }) console.log(arr)
Note: Mutating an array inside loop is not a good idea 注意:在循环内更改数组不是一个好主意
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.