2

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:

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]

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.

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.

5 Answers 5

4

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

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()

Sign up to request clarification or add additional context in comments.

2 Comments

great! it helped me a lot
@FeRcHo Happy to help. I also added the answer to new question update.
2

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

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));

1 Comment

great has helped me a lot, now that I'm just learning javascripts, add one more detail to my question I hope you can help me thanks
2

You can use filter to check if id property of element in arr2 matches to the current element in 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 ]

Comments

1

You can use .filter() and .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().
  • Filter elements using .filter() and .includes() to filter only those elements of second array that doesn't exists in newly created array.

Docs:

Comments

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.

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; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.