简体   繁体   English

如何从另一个对象数组更新一个对象数组中不存在的值?

[英]How to update values not present in one array of objects from another array of objects?

I have two arrays of objects which contain a huge amount of data. 我有两个包含大量数据的对象数组。 The structure of these two arrays goes something like this. 这两个数组的结构类似这样。

arr1 = [
  {x: 1, y: '2018-01-01'}, 
  {x: 2, y: '2018-01-02'},           
  {x: 3, y: '2018-01-03'},
  {x: 5, y: '2018-01-05'},
....
]
arr2 = [
  {x: 1, y: '2018-01-01'}, 
  {x: 2, y: '2018-01-02'},           
  {x: 3, y: '2018-01-03'},
  {x: 4, y: '2018-01-04'},
  {x: 5, y: '2018-01-05'},
  {x: 6, y: '2018-01-08'}
]

I want to update arr2 in such a way that it updates the array of objects with values that are only present in arr1 and drop any values not present in arr1. 我要更新arr2 ,使其使用仅出现在arr1中的值更新对象数组,并删除任何不在arr1中的值。 Note, I want to update the original arr2 and not return a new array. 注意,我想更新原始的arr2而不返回新的数组。

I tried iterating through individual arrays and remove values not present but not luck. 我尝试遍历单个数组并删除不存在但不走运的值。

You could get a map and iterate from the end for splicing unknown items or update changed values. 您可以获得一张地图,并从头开始进行迭代以拼接未知项或更新更改后的值。

 var arr1 = [{ x: 1, y: '2018-01-01x' }, { x: 2, y: '2018-01-02' }, { x: 3, y: '2018-01-03' }, { x: 5, y: '2018-01-05' }], arr2 = [{ x: 1, y: '2018-01-01' }, { x: 2, y: '2018-01-02' }, { x: 3, y: '2018-01-03' }, { x: 4, y: '2018-01-04' }, { x: 5, y: '2018-01-05' }, { x: 6, y: '2018-01-08' }], map = arr1.reduce((m, { x, y }) => m.set(x, y), new Map), i = arr2.length; while (i--) { if (map.has(arr2[i].x)) { if (map.get(arr2[i].x) !== arr2[i].y) { arr2[i].y = map.get(arr2[i].x); } } else { arr2.splice(i, 1); } } console.log(arr2); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从一个对象数组中获取值到另一个对象数组中 - How to get values from one array of objects into another array of objects 如何将类型4中的值从一个对象数组复制到另一个对象数组中? - How to copy values from one array of objects to another array of objects in typescript, angular 4? 从另一个数组更新对象数组 - Update objects array from another array 如何在 JavaScript 中从另一个数组中过滤一个数组中的对象? - How to filter objects from one array from another array in JavaScript? 如何根据另一个对象数组的值获取一个对象数组键的值? - how to get the values of one array of objects key based on values of another array of objects? 如何将新字段添加到对象数组,或将值从一个数组插入到另一个数组-JS - How to add new fields to an Array of Objects, or insert values from one array to another - JS Typescript:如何在另一个数组的对象数组中设置值 - Typescript: How to set values of in an array of objects from another array 如何按照包含每个对象中存在的属性的唯一值的另一个数组的顺序对对象数组进行排序? - How to sort an array of objects in the order of another array containing the unique values of a property present in each object? 从 JavaScript 中的另一个对象数组替换一个对象数组的值 - Replace values of an array of objects from another array of objects in JavaScript 从嵌套在另一个对象数组中的对象数组中访问值 - accessing values from an array of objects nested within another array of objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM