0

I have this array:

let idsClass = [1,2]

and i have this other array:

let idsQuiz = [{id: 1}]

I need to know the id numbers that have in idsClass that is missing in idsQuiz.

So, i need an new array containing this elements, for this example, this is the correct output:

[2]

I tryed something like:

 idsClass.forEach(item => { if (!idsQuizz.includes({id:item})) { console.log('dont find this: ' + item) } })

But my console is printing the two elements

bla
  • 995
  • 3
  • 11
  • 44
  • Does this answer your question? [How to merge two arrays in JavaScript and de-duplicate items](https://stackoverflow.com/questions/1584370/how-to-merge-two-arrays-in-javascript-and-de-duplicate-items) – fingeron Jan 21 '20 at 18:56
  • When I copy-paste your code and run it, the value of `idsQuiz` is `[1,2]` – Simon Crane Jan 21 '20 at 18:58
  • sorry i forgot to specify that the second array is an array of object – bla Jan 21 '20 at 19:09
  • So is the expected output `[1,2]` or `[{id: 1},{id: 2}]`? – Klaycon Jan 21 '20 at 19:10
  • this problem is very poorly defined. The title says to include elements in the array if it's *not* in the other array, yet the question says to put in the array *all* elements from the other array. The expected output is integers, yet the starting array is objects. This is certainly being overcomplicated. – Klaycon Jan 21 '20 at 19:13
  • Some more sample input/output data would help clarify the problem I think. – Klaycon Jan 21 '20 at 19:14
  • @Klaycon you are right, i will edit with more informations – bla Jan 21 '20 at 19:15
  • @Klaycon please check my updte, i put the correct informations – bla Jan 21 '20 at 19:19

3 Answers3

3

Try to use filter built-in function to check existence.

let idsClass = [1,2];


let idsQuiz = [{id: 1}];

idsClass.forEach(item => {
  if (idsQuiz.filter(e => e.id === item).length === 0) {
    idsQuiz.push({id: item});
  }
});

console.log(idsQuiz); // output is [{"id": 1}, {"id": 2}]
Rise
  • 1,493
  • 9
  • 17
  • There's a way to make this work if the second array is an array of object? My second element is an array of object and i try: idsClass.forEach(item => { if (!idsQuizz.includes({id:item})) { console.log('dont find this: ' + item) } }) but is returning the two elements – bla Jan 21 '20 at 19:07
  • Yes, there is. Check it's key with includes function. if (! idsQuiz.includes(item.id)) { – Rise Jan 21 '20 at 19:21
  • if i put this way, item.id is undefined, i need to compare the key of the idsQuiz.id – bla Jan 21 '20 at 19:27
  • 1
    You could use filter function in case. – Rise Jan 21 '20 at 19:31
2

basic solution

let idsClass = [1,2,2,4,5,6];
let idsQuiz = [1,4];
for(let i=0;i<idsClass.length;i++){
    let j=0;
    for(;j<idsQuiz.length;j++){
        if(idsClass[i] === idsQuiz[j]){
            break;
        }
    }
    if(j === idsQuiz.length) {
        idsQuiz.push(idsClass[i]);
    }
}
console.log(idsQuiz);

solution using inbuilt methods

let idsClass = [1,2,2,4,5,6];
let idsQuiz = [1,4];

// Adds all elements of idsClass into idsQuiz. may contain duplicates
idsQuiz.push(...idsClass);

//Removes duplicates from idsQuiz
idsQuiz = idsQuiz.filter((val,index,self) => self.indexOf(val) === index);
console.log(idsQuiz);

your code adds element into idsQuiz whenever an element in idsClass doesn't match with an element in idsQuiz. Instead, iterate through whole idsQuiz list and then add if it doesn't exist for each idsClass element.

idsClass = [1,2,3]
idsQuiz = [1]

i = 0, j=0 (no element is added in idsQuiz)
i=1, j=0 (2 is added in idsQuiz)
i=1, j=1 (no element is added in idsQuiz)
i=2, j=0 (3 is added in idsQuiz)
i=2, j=1 (3 is added in idsQuiz)
i=2, j=2 (no element is added in idsQuiz)
i=2, j=3 (no element is added in idsQuiz)

Mike Reddington
  • 192
  • 2
  • 15
1

There's probably a more efficient method, but I think this way is easy to understand:

let idsClass = [1,2,3,4,5,6];
let idsQuiz = [{id: 1},{id: 4}];

let result = idsClass.filter(n => !idsQuiz.some(o => o.id == n));

console.log(result);

Simply use a filter() on the idsClass. filter will return a new array with only the elements that pass the inner function. In this case, it only returns elements that aren't in the idsQuiz.

Klaycon
  • 10,599
  • 18
  • 35