比较具有不同属性数量的两个数组,并使用javascript在条件下将第三个数组映射到比较值

Posted

技术标签:

【中文标题】比较具有不同属性数量的两个数组,并使用javascript在条件下将第三个数组映射到比较值【英文标题】:compare two arrays having different number of properties and mapping a third array with compared values on condition using javascript 【发布时间】:2018-09-30 06:57:39 【问题描述】:

我有 2 个数组

var array1 = ["1", "2", "3", "4", "5", "6"]

var array2 = [
   offId: "4", offname: "four" ,
   offId: "9", offname: "nine" ,
   offId: "15", offname: "fifteen" ,
   offid: "3", offname: "three" ,
   offId: "1", offname: "one" ,
   offId: "0", offname: "zero" ,
   offId: "8", offname: "eight" ,
   offId: "10", offname: "ten" ,
]

我需要将两个数组与 offId 的值进行比较,结果数组应该是

var array3 = [
   offId: "1", offname: "one" ,
   offId: "2", offname: "" ,
   offId: "3", offname: "three" ,
   offId: "4", offname: "four" ,
   offId: "5", offname: "" ,
   offId: "6", offname: "" ,
]

我怎样才能做到这一点(数组的长度可能相同或不同)

【问题讨论】:

'offId' !== 'offid' @NinaScholz 两者都相等 'offId' ='offId'..edited @HKI345 检查我的回答我认为它会解决你的问题。 【参考方案1】:

您可以使用Array.reducearray1array2 创建array3

步骤如下:

acc(累加数组)最初将包含空数组。 使用Array.find 检查curr(当前变量)是否在array2 中。 如果a.offId 匹配curr 值,Array.find 将返回对象。 如果找到obj,则将obj推入acc(累加数组) 否则将offId: curr, offname: '' 推送到数组中

var array1 = ["1", "2", "3", "4", "5", "6"]

var array2 = [
   offId: "4", offname: "four" ,
   offId: "9", offname: "nine" ,
   offId: "15", offname: "fifteen" ,
   offId: "3", offname: "three" ,
   offId: "1", offname: "one" ,
   offId: "0", offname: "zero" ,
   offId: "8", offname: "eight" ,
   offId: "10", offname: "ten" ,
]

var array3 = array1.reduce((acc, curr) => 
  var obj = array2.find(a => a.offId === curr);
  if (obj) 
    acc.push(obj);
   else 
    acc.push( offId: curr, offname: "");
  
  return acc;
, []);

console.log(array3);

【讨论】:

【参考方案2】:

您可以使用Map 并使用存储的对象或新对象。

var array1 = ["1", "2", "3", "4", "5", "6"],
    array2 = [ offId: "4", offname: "four" ,  offId: "9", offname: "nine" ,  offId: "15", offname: "fifteen" ,  offId: "3", offname: "three" ,  offId: "1", offname: "one" ,  offId: "0", offname: "zero" ,  offId: "8", offname: "eight" ,  offId: "10", offname: "ten" ],
    map = array2.reduce((m, o) => m.set(o.offId, o), new Map),
    result = array1.map(offId => map.get(offId) ||  offId, offname: '' );

console.log(result);
.as-console-wrapper  max-height: 100% !important; top: 0; 

【讨论】:

【参考方案3】:

var array1 = ["1", "2", "3", "4", "5", "6"];

var array2 = [
   offId: "4", offname: "four" ,
   offId: "9", offname: "nine" ,
   offId: "15", offname: "fifteen" ,
   offId: "3", offname: "three" ,
   offId: "1", offname: "one" ,
   offId: "0", offname: "zero" ,
   offId: "8", offname: "eight" ,
   offId: "10", offname: "ten" ,
];

var array3 = array1.map(val => array2.filter(obj => obj.offId == val)[0] ||  offId: val, offname: "" );

console.log(array3);

【讨论】:

该方法的性能最差,因为您使用过滤器并且这会迭代数组的所有元素。 find 迭代直到找到一个元素。或使用哈希表,如对象或地图,无需迭代即可快速访问。【参考方案4】:

最简单的方法是简单地 filter() 第二个数组:

const array3 = array2.filter(v => array1.includes(v.offId));

这将遍历array2 中的每个元素,并且只将具有offId 的元素留在array1 中。

如果您需要对其进行排序,则只需在其末尾添加一个排序:

const array3 = array2.filter(v => array1.includes(v.offId))
  .sort((a, b) => b.offId > a.offId ? 1 : a.offId < b.offId ? -1 : 0);

如果您需要对它进行数字排序(即,您希望 2 在 10 之前),那么您需要转换它们的值,但是您可以减去它们:

const array3 = array2.filter(v => array1.includes(v.offId))
  .sort((a, b) => parseInt(b.offId) - parseInt(a.offId));

【讨论】:

以上是关于比较具有不同属性数量的两个数组,并使用javascript在条件下将第三个数组映射到比较值的主要内容,如果未能解决你的问题,请参考以下文章

如何在objective-c中比较两个具有很多属性的对象

比较两个数组并返回重复值

text 比较两个具有相同项但不同顺序的数组

查找最多具有 k 个奇数元素的不同连续子数组的数量

按匹配不同数组的属性过滤字典数组

比较两个字节数组的最快方法是啥?