简体   繁体   English

如果元素在另一个对象数组中,则删除包含对象数组内部数组的对象属性

[英]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 伪码

  1. If id matches in both the array then take that object from the arr array. 如果id在两个数组中都匹配,则从arr数组中获取该对象。
  2. Use Object.keys to get array of keys For example the array will be now 使用Object.keys获取键数组例如,该数组现在是

    ['id','array0','array1'] [ 'ID', 'ARRAY0', 'ARRAY1']

  3. 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.

相关问题 在另一个数组内的JavaScript对象数组中按属性查找对象 - Find object by property in an array of JavaScript objects inside another array 如果存在于另一个对象数组中,则从对象数组内部的数组中删除元素 - Remove element from array inside array of objects if present in another array of objects 在另一个对象数组中的对象数组中搜索 object - Search for an object inside an array of objects inside another array of objects 包含对象的对象数组到平面 object 数组 - Array of objects containing objects to flat object array javascript通过另一个对象数组内的属性检查添加或替换对象 - javascript add or replace an object by property check inside another array of objects 将包含对象的object转换为包含对象的数组 - Convert object containing objects to array containing objects 将包含空数组的对象数组映射为属性会忽略整个对象 - Mapping an array of objects containing an empty array as property ignores the whole object 按对象属性删除对象数组的 doublon - Remove doublon for an array of objects by object property 从对象数组中的对象中删除属性 mongoose - remove a property from an object in an array of objects mongoose 如何按照包含每个对象中存在的属性的唯一值的另一个数组的顺序对对象数组进行排序? - How to sort an array of objects in the order of another array containing the unique values of a property present in each object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM