将数组的值与对象数组的键进行比较

时间:2023-01-16 11:32:31

As the question indicates, I need to compare the values of an array of this type [1,2,3], with the values of a key of an array of objects [{id: 1, name: 'jhon'}, {id: 2 , name: 'max'}] in this case I want to compare it with the value of the id key, I write the following example of what I need:

如问题所示,我需要将此类型数组的值[1,2,3]与对象数组的键值[{id:1,name:'jhon'},{进行比较id:2,name:'max'}]在这种情况下,我想将它与id键的值进行比较,我写下我需要的示例:

if I have this array of objects:

如果我有这个对象数组:

[
   {
      id: 1
      name: 'jhon'
   },
   {
     id: 2,
     name: 'max'
   },
   {
     id: 3,
     name: 'fer'
   }
]

and I have this array:

我有这个数组:

[1,2,3,4,5]

I need to compare them and obtain the values that are not present in the array of objects, in the previous example the values 4 and 5 are not present in the array of objects, so I need to obtain a new array or the same, but with the values that do not they were, [4,5]

我需要比较它们并获得对象数组中不存在的值,在前面的例子中,值4和5不存在于对象数组中,所以我需要获得一个新的数组或者相同,但是他们没有的价值观,[4,5]

NOTE: this should also work if I have an array, only with numbers that are not present, for example [8,9], they should return those same values.

注意:如果我有一个数组,只有不存在的数字,例如[8,9],它也应该有效,它们应该返回相同的值。

EDIT: it is possible to obtain in a new array those values that are only present in the array of objects. For example, with the arrays of the previous code:

编辑:可以在新数组中获得仅存在于对象数组中的值。例如,使用前面代码的数组:

[{id: 1, name: 'jhon'}, {id: 2, name: 'max'}, {id: 3, name: 'fer'}]

now this array would have the following values [1,4,5] with the previous answers I get the following array [4,5] what is correct. how can I get the values [2,3] in a new array.

现在这个数组将具有以下值[1,4,5]与之前的答案我得到以下数组[4,5]什么是正确的。如何在新数组中获取值[2,3]。

5 个解决方案

#1


2  

You can use filter and find

您可以使用过滤器并查找

let arr1 = [{id: 1,name: 'jhon'},{id: 2,name: 'max'},{id: 3,name: 'fer'}]
let arr2 = [1, 2, 3, 4, 5];

let result = arr2.filter(o => !arr1.find(x => x.id === o));

console.log(result);


Update: This is basically the reverse of the first example. You filter the arr2 and check if the id exists on arr1. Then, use map to return the id

更新:这基本上与第一个示例相反。您过滤arr2并检查arr1上是否存在id。然后,使用map返回id

let arr1 = [{id: 1,name: 'jhon'},{id: 2,name: 'max'},{id: 3,name: 'fer'}]
let arr2 = [1,4,5];

let result = arr1.filter(o => !arr2.find(x => x === o.id)).map(o=>o.id);

console.log(result);

Doc: filter(), find()

Doc:filter(),find()

#2


2  

Create a Set from the ids of arr1 using Array.map(). Then filter arr2 by checking if the Set has the number:

使用Array.map()从arr1的id创建一个Set。然后通过检查Set是否具有数字来过滤arr2:

const arr1 = [{"id":1,"name":"jhon"},{"id":2,"name":"max"},{"id":3,"name":"fer"}];
const arr2 = [1,2,3,4,5];
const arr3 = [1, 5];

const difference = (arr1, arr2, key) => {
  const arr1Values = new Set(key ? arr1.map(({ [key]: v }) => v) : arr1);

  return arr2.filter((n) => !arr1Values.has(n));
};

// array of primitives and array of objects
console.log(difference(arr1, arr2, 'id'));

// two arrays of primitives 
console.log(difference(arr3, arr2));

#3


2  

You can use filter to check if id property of element in arr2 matches to the current element in arr2

您可以使用filter来检查arr2中元素的id属性是否与arr2中的当前元素匹配

const arr1 = [{"id":1,"name":"jhon"},{"id":2,"name":"max"},{"id":3,"name":"fer"}];

const arr2 = [8,9];
var diffArray = arr2.filter(x => !arr1.filter(y => y.id === x).length);
console.log(diffArray);

Answer [ 8,9 ]

回答[8,9]

#4


1  

You can use .filter() and .includes():

您可以使用.filter()和.includes():

let arr1 = [{id: 1,name: 'jhon'},{id: 2, name: 'max'},{id: 3,name: 'fer'}],
    arr2 = [1, 2, 3, 4, 5];

let result = (
    ids => arr2.filter(n => !ids.includes(n))
)(arr1.map(({id}) => id));

console.log(result);

Description:

描述:

  • Create a new array by extracting only ids from first array using .map().
  • 通过使用.map()仅从第一个数组中提取ID来创建新数组。
  • Filter elements using .filter() and .includes() to filter only those elements of second array that doesn't exists in newly created array.
  • 使用.filter()和.includes()过滤元素,仅过滤新创建的数组中不存在的第二个数组的元素。

Docs:

文档:

#5


1  

You could take a set with the given ids and then delete the ones of the object. Later return the array with the leftover ids.

您可以使用给定的ID获取一个集合,然后删除该对象的集合。稍后使用剩余的id返回数组。

var data = [{ id: 1, name: 'jhon' }, { id: 2, name: 'max' }, { id: 3, name: 'fer' }],
    ids = [1, 2, 3, 4, 5],
    result = Array.from(data.reduce((s, { id }) => (s.delete(id), s), new Set(ids)));

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

#1


2  

You can use filter and find

您可以使用过滤器并查找

let arr1 = [{id: 1,name: 'jhon'},{id: 2,name: 'max'},{id: 3,name: 'fer'}]
let arr2 = [1, 2, 3, 4, 5];

let result = arr2.filter(o => !arr1.find(x => x.id === o));

console.log(result);


Update: This is basically the reverse of the first example. You filter the arr2 and check if the id exists on arr1. Then, use map to return the id

更新:这基本上与第一个示例相反。您过滤arr2并检查arr1上是否存在id。然后,使用map返回id

let arr1 = [{id: 1,name: 'jhon'},{id: 2,name: 'max'},{id: 3,name: 'fer'}]
let arr2 = [1,4,5];

let result = arr1.filter(o => !arr2.find(x => x === o.id)).map(o=>o.id);

console.log(result);

Doc: filter(), find()

Doc:filter(),find()

#2


2  

Create a Set from the ids of arr1 using Array.map(). Then filter arr2 by checking if the Set has the number:

使用Array.map()从arr1的id创建一个Set。然后通过检查Set是否具有数字来过滤arr2:

const arr1 = [{"id":1,"name":"jhon"},{"id":2,"name":"max"},{"id":3,"name":"fer"}];
const arr2 = [1,2,3,4,5];
const arr3 = [1, 5];

const difference = (arr1, arr2, key) => {
  const arr1Values = new Set(key ? arr1.map(({ [key]: v }) => v) : arr1);

  return arr2.filter((n) => !arr1Values.has(n));
};

// array of primitives and array of objects
console.log(difference(arr1, arr2, 'id'));

// two arrays of primitives 
console.log(difference(arr3, arr2));

#3


2  

You can use filter to check if id property of element in arr2 matches to the current element in arr2

您可以使用filter来检查arr2中元素的id属性是否与arr2中的当前元素匹配

const arr1 = [{"id":1,"name":"jhon"},{"id":2,"name":"max"},{"id":3,"name":"fer"}];

const arr2 = [8,9];
var diffArray = arr2.filter(x => !arr1.filter(y => y.id === x).length);
console.log(diffArray);

Answer [ 8,9 ]

回答[8,9]

#4


1  

You can use .filter() and .includes():

您可以使用.filter()和.includes():

let arr1 = [{id: 1,name: 'jhon'},{id: 2, name: 'max'},{id: 3,name: 'fer'}],
    arr2 = [1, 2, 3, 4, 5];

let result = (
    ids => arr2.filter(n => !ids.includes(n))
)(arr1.map(({id}) => id));

console.log(result);

Description:

描述:

  • Create a new array by extracting only ids from first array using .map().
  • 通过使用.map()仅从第一个数组中提取ID来创建新数组。
  • Filter elements using .filter() and .includes() to filter only those elements of second array that doesn't exists in newly created array.
  • 使用.filter()和.includes()过滤元素,仅过滤新创建的数组中不存在的第二个数组的元素。

Docs:

文档:

#5


1  

You could take a set with the given ids and then delete the ones of the object. Later return the array with the leftover ids.

您可以使用给定的ID获取一个集合,然后删除该对象的集合。稍后使用剩余的id返回数组。

var data = [{ id: 1, name: 'jhon' }, { id: 2, name: 'max' }, { id: 3, name: 'fer' }],
    ids = [1, 2, 3, 4, 5],
    result = Array.from(data.reduce((s, { id }) => (s.delete(id), s), new Set(ids)));

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