Home > Mobile >  Compare arrA(str) and arrB(obj) and return a list of objects from arrB that match both its property
Compare arrA(str) and arrB(obj) and return a list of objects from arrB that match both its property

Time:12-05

Need to find the most efficient way to compare arrA and arrB and return an array of arrB's elements that match both keys to strings found in arrA.

There will only ever be two properties per object in arrB, but the number of elements in arrA will shift dramatically depending on the number of elements in arrB. Also, arrB's number of elements is unknown.

const arrA = ["green", "blue", "orange"]

const arrB = [
   { orange: 4, green: 4},
   { green: 0, yellow: 0},
   { yellow: 1, orange: 4 },
   { blue: 2, green: 1 },
   { blue: 2, yellow: 1 }, 
   { green: 3, yellow: 2 },
   { green: 1, blue: 3},
   { green: 5, yellow: 2 }, 
   { green: 5, blue: 2}
 ]

The result would look like:

var arrC= [
 {orange: 4, green: 4 },
 {blue: 2, green: 1 },
 {green: 1, blue: 3 },
 {green: 5, blue: 2 }
]

I've attempted a mix of ideas that look like varying options from below but clearly none of them function because they are a mix of code and pseudocode:

const compare = (arrA, arrB) => {
   const color_obj = arrB[i]
   (color_obj) => {
      const [[color1, val1], [color2, val2]] = Object.entries(color_obj)

   for (let i=0; i<arrA; i  ) {
     if(arrA[i] === color1 && arrA[i 1] === color2 || arrA[i] === color2 && arrA[i 1] === color1)
       filteredColorObjects   arrb[obj]
   }
   }
}

Or

const compare = arrA.filter((e) => Object.entries(arrB.includes(e[1] && e[2]), [])

Or

const compare = arrB.filter(o => arrA.includes(o.arrB[i])).map(o => o[key] && o.arrB[i])).map(o => o[key]);

Or

const compare = arrA.filter((e) {
    return !!arrB.find(function(o) {
        return arrB[i] === e;
    });

CodePudding user response:

Try this:

var arrC = []
arrB.forEach( e => {
    if(Object.keys(e).every(i => arrA.includes(i))){
        arrC.push(e);
    }
})

.forEach: iterates over all elements of the array (arrB)

Object.keys(e): gets all keys in the object e as array

.every: iterates over all elements of the array (Object.keys(e)) and return true only if all iterations returned true.

#So here we are iterating over the keys of the objects inside arrB.

.includes: will return true if the value we send exists in the array

Now if all the keys of the object are in arrA, we push the object to arrC.

CodePudding user response:

@busard's answer seems fine to me, just posting this to note that you probably can use filter (as you do in your question) if you want and using Set may be a bit more efficient, but you may want to test that.

const arrA = ["green", "blue", "orange"];
const arrB = [ { orange: 4, green: 4}, { green: 0, yellow: 0}, { yellow: 1, orange: 4 }, { blue: 2, green: 1 }, { blue: 2, yellow: 1 }, { green: 3, yellow: 2 }, { green: 1, blue: 3}, { green: 5, yellow: 2 }, { green: 5, blue: 2}, ];

const setA = new Set(arrA);

const arrC = arrB.filter(
  (item) => Object.keys(item).every( (k) => setA.has(k) )
);

console.log(arrC);

  • Related