foodiepanda
foodiepanda

Reputation: 138

How to change key value based on comparison of array of objects in jQuery?

I have two arrays in jQuery as below.

var arr1 = [{A: "t1"},{A: "t3"}];

var arr2 = [{A: "t1",B:""},{A: "t2",B:""},{A: "t3",B:""},{A: "t4",B:""},{A: "t5",B:""},{A: "t6",B:""}];

var arr3 = []; //Only for output

Now, the output in arr3 should be:

So, the output I want in arr3 should be:

arr3 = [{A: "t1",B:"Yes"},{A: "t2",B:"No"},{A: "t3",B:"Yes"},{A: "t4",B:"No"},{A: "t5",B:"No"},{A: "t6",B:"No"}];

This is what I have tried so far

var arr1 = [{A: "t1"},{A: "t3"}];
var arr2 = [{A: "t1",B:""},{A: "t2",B:""},{A: "t3",B:""},{A: "t4",B:""},{A: "t5",B:""},{A: "t6",B:""}];
var arr3 = []; //Only for output

arr1.forEach(function(x) {
  arr2.forEach(function(y) {
    if (x.A == y.A) {
      arr3.push({
        A: x.A,
        B: "Yes"
      });
    } else {
      arr3.push({
        A: x.A,
        B: "No"
      });
    }
  });
});

console.log(arr3);

//Output
0: {A: "t1", B: "Yes"}
1: {A: "t1", B: "No"}
2: {A: "t1", B: "No"}
3: {A: "t1", B: "No"}
4: {A: "t1", B: "No"}
5: {A: "t1", B: "No"}
6: {A: "t3", B: "No"}
7: {A: "t3", B: "No"}
8: {A: "t3", B: "Yes"}
9: {A: "t3", B: "No"}
10: {A: "t3", B: "No"}
11: {A: "t3", B: "No"}

Issue here is that it repeats for each item in arr1, and count gets doubled. I know it should break somewhere between if and else, so that it does not repeat.

I have tried checking below links too, but couldn't get much help.

filter array of objects based on separate object keys, values

js - How to change one key's value based on another key's value in array of objects

Object comparison based on keys

compare array of objects

Thanks in advance.

Upvotes: 2

Views: 837

Answers (2)

mplungjan
mplungjan

Reputation: 178350

Use map and find if the arrays are not huge.

Saves a reduce and push

var arr1 = [{ A: "t1" }, { A: "t3" }];
var arr2 = [{ A: "t1", B: ""}, { A: "t2", B: "" }, { A: "t3", B: "" }, { A: "t4", B: "" }, { A: "t5", B: "" }, { A: "t6", B: "" }];
var arr3 = arr2.map(
  item2 => ({
    A: item2.A,
    B: arr1.find(item1 => item1.A === item2.A) ? 'Yes' : 'No'
  }));

console.log(arr3);

Upvotes: 2

Nick
Nick

Reputation: 147216

You can make an array of the A values in arr1 using Array.reduce(), then use Array.includes to check if each of the A values in arr2 is present in that array, using that to decide whether to set B to Yes or No:

var arr1 = [{ A: "t1" }, { A: "t3" }];
var arr2 = [{ A: "t1", B: ""}, { A: "t2", B: "" }, { A: "t3", B: "" }, { A: "t4", B: "" }, { A: "t5", B: "" }, { A: "t6", B: "" }];
var arr3 = []; //Only for output

var arr1values = arr1.reduce((c, v) => c.concat([v.A]), []);

arr2.forEach(function(y) {
  arr3.push({
    A: y.A,
    B: arr1values.includes(y.A) ? 'Yes' : 'No'
  });
});

console.log(arr3);

Upvotes: 1

Related Questions