Code Bug Fix: How to check if one array of objects value with another array of objects value, if found then hide something?

Original Source Link

I am trying to check one array of objects value exists in another array of objects value (Means if both values matches then i am disabling the table row). So for that how do i check both array of objects values. See my below two array objects.

const array1 = [
{
id: "8a8080877134b405017134c1adb3004f",
name: "AWS API1"
},
{
id: "8a8080b6720900d301720935a7120000",
name: "AWS API3"
},
{
id: "8a80808271773317017177848a5106d1",
name: "AZURE API1"
}
];

const array2 = [
{
apiId: "8a8080877134b405017134c1adb4444f",
apiName: "AWS API2"
},
{
apiId: "ass34dgdfgfdgfdg35435ERF",
apiName: "AZURE API1"
},
{
apiId: "dfdfdaggfdgdfg4324564",
apiName: "AWS API1"
}
];

From the above 2 array of objects, i want to check if the array2.apiName matches with array1.name then i want to disable table row. So how to achieve it?

See the below code.

const r = array1.filter((elem) => !array2.find(({ apiName }) => elem.name === apiName));
console.log(r);

You could use array some method on both the arrays and introduce a new property disable into the first array. And set disable property to true if it’s name is present in second array’s apiName property.

And in the template use the disable property to disable/enable the rows.

But beware, the some method will iterate each element of the first array with all the elements of the second array. So it could be a relatively heavy process for considerably large arrays.

var array1 = [
  { id: "8a8080877134b405017134c1adb3004f", name: "AWS API1" },
  { id: "8a8080b6720900d301720935a7120000", name: "AWS API3" },
  { id: "8a80808271773317017177848a5106d1", name: "AZURE API1" }
];

var array2 = [
  { apiId: "8a8080877134b405017134c1adb4444f", apiName: "AWS API2" },
  { apiId: "ass34dgdfgfdgfdg35435ERF", apiName: "AZURE API1" },
  { apiId: "dfdfdaggfdgdfg4324564", apiName: "AWS API1" }
];

array1.some(a1 => {
  array2.some(a2 => {
    if (a1.name === a2.apiName) {
      a1.disable = true;
    } else {
      if (a1.disable !== true) {
        a1.disable = false;
      }
    }
  })
});

console.log(array1);
Tagged : / /

Leave a Reply

Your email address will not be published. Required fields are marked *