2

I have two JS arrays. They are arr1 and arr2. I want to create another array called arr3. Now it wants to compare arr1 and arr2 and insert all elements which arr2 contains and arr1 not contain.

Example:

arr1:

  0:
    des: "cont1"
    note: "cont1"
    pro_code: "XXY"
  1:
    des: "cont2"
    note: "cont2"
    pro_code: "NNB"
  2:
    des: "cont4"
    note: "cont4"
    pro_code: "QQA"
  3:
    des: "cont5"
    note: "cont5"
    pro_code: "GFD"

arr2:

  0:
    des: "cont1"
    note: "cont1"
    pro_code: "XXY"
  1:
    des: "cont2"
    note: "cont2"
    pro_code: "NNB"
  2:
    des: "cont3"
    note: "cont3"
    pro_code: "QAS"

In arr2 contains pro_code: QAS. But it is not in arr1. So it should be contained in arr3.

In arr1 contains arr1[4] pro_code: GFD. it should not be contained in arr3. Becase aar1 can contain additional elements.

Here arr2 must contain elements that are in arr1. There can not additional elements in arr2.

Expected output:

const arr3 = [{
  des: "cont3",
  note: "cont3",
  pro_code: "QAS"
}

My tried code, not workes. Please help me to solve this.

2
  • please add the wanted result. Commented Oct 19, 2020 at 8:08
  • @NinaScholz I updated the question with Expected output: I am using this in vue computed: there some() is not working. Commented Oct 19, 2020 at 8:14

3 Answers 3

2

You could take a Set for all pro_code values and filter the second array by checking if the value is not in the set.

const
    array1 = [{ des: "cont1", note: "cont1", pro_code: "XXY" }, { des: "cont2", note: "cont2", pro_code: "NNB" }, { des: "cont4", note: "cont4", pro_code: "QQA" }, { des: "cont5", note: "cont5", pro_code: "GFD" }],
    array2 = [{ des: "cont1", note: "cont1", pro_code: "XXY" }, { des: "cont2", note: "cont2", pro_code: "NNB" }, { des: "cont3", note: "cont3", pro_code: "QAS" }],
    pro_codes = new Set(array1.map(({ pro_code }) => pro_code)),
    result = array2.filter(({ pro_code }) => !pro_codes.has(pro_code));

console.log(result);

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

Comments

1

In short, your requirement is to filter the arr2 and keep elements that are not in arr1.

const arr3 = arr2.filter(function(arr2item) {
  // only keep this item if it is not in arr1
  return !arr1.some(function(arr1item) {
    return arr1item.pro_code === arr2item.pro_code;
  })
});

const arr1 = [{des: "cont1", note: "cont1", pro_code: "XXY"}, {des: "cont2", note: "cont2", pro_code: "NNB"}, {des: "cont4", note: "cont4", pro_code: "QQA"}, {des: "cont5", note: "cont5", pro_code: "GFD"}];
const arr2 = [{des: "cont1", note: "cont1", pro_code: "XXY"}, {des: "cont2", note: "cont2", pro_code: "NNB"}, {des: "cont3", note: "cont3", pro_code: "QAS"}]

const arr3 = arr2.filter(function(arr2item) {
  return !arr1.some(function(arr1item) {
    return arr1item.pro_code === arr2item.pro_code;
  })
});

console.log(arr3);

1 Comment

@connexo i am not comparing the objects, but the pro_code string property.
1

Here's another optimized solution

const arr1 = [{
  des: "cont1",
  note: "cont1",
  pro_code: "XXY"
}, {
  des: "cont2",
  note: "cont2",
  pro_code: "NNB"
}, {
  des: "cont4",
  note: "cont4",
  pro_code: "QQA"
}, {
  des: "cont5",
  note: "cont5",
  pro_code: "GFD"
}];

const arr2 = [{
  des: "cont1",
  note: "cont1",
  pro_code: "XXY"
}, {
  des: "cont2",
  note: "cont2",
  pro_code: "NNB"
}, {
  des: "cont3",
  note: "cont3",
  pro_code: "QAS"
}];

const results = arr2.filter(({ pro_code: id1 }) => !arr1.some(({ pro_code: id2 }) => id2 === id1));

console.log(results);

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.