Saif
Saif

Reputation: 285

Comparing two arrays and filtering one array upon this in javascript and react

I am trying to compare two arrays of objects as following :

const obj1 = [
        {name:"Mark"},
        {name:"David"},
        {name:"Ellie"},
        {name:"Zuank"},
        {name:"Philip"},
        {name:"Den"},
    ]
    const obj2 = [
        {name:"Mark"},
        {name:"David"},
        {name:"Zuank"},
        {name:"Philip"},
    ]

I want to check if the name of every object exists in the second object or not. If yes, I want to push that object to a new array with a new property named "Matched" added to each object with a value of "true". Otherwise, the Matched property will be false. This is the final result I want to achieve :

  const res = [
        { name: "Mark", matched: true },
        { name: "David", matched: true },
        { name: "Ellie", matched: false },
        { name: "Zuank", matched: true },
        { name: "Philip", matched: true },
        { name: "Den", matched: false },
    ]

--Edit Here is what I have tried so far guys :

obj1.map((element, index) => {
        if (obj2[index].name === element.name) {
            resArr.push({ name: element.name, matched: true })
        }
        else {
            resArr.push({ name: element.name, matched: false })
        }
    })

Upvotes: 2

Views: 7420

Answers (6)

Hayden Tofts
Hayden Tofts

Reputation: 56

Just map through obj1 and while mapping filter obj2 to check if the current name is in obj2. If it is then push the name along with the matched value of true into obj3. If it isn't then push the name along with the matched value of false into obj3.

const obj1 = [
  {name:"Mark"},
  {name:"David"},
  {name:"Ellie"},
  {name:"Zuank"},
  {name:"Philip"},
  {name:"Den"}
]
const obj2 = [
  {name:"Mark"},
  {name:"David"},
  {name:"Zuank"},
  {name:"Philip"}
]
let obj3 = []

obj1.map(function(a) {
 let matched = obj2.filter(b => a.name === b.name);
 if (matched.length) {
  obj3.push({name: a.name, matched: true});
 } else {
  obj3.push({name: a.name, matched: false});
 }
})

console.log(obj3);

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386560

You could take a Set for all names and map the new property by checking the set.

const
    array1 = [{ name: "Mark" }, { name: "David" }, { name: "Ellie" }, { name: "Zuank" }, { name: "Philip" }, { name: "Den" }],
    array2 = [{ name: "Mark" }, { name: "David" }, { name: "Zuank" }, { name: "Philip" }],
    names = new Set(array2.map(({ name }) => name)),
    result = array1.map(o => ({ ...o, matched: names.has(o.name) }));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 0

Jonas Wilms
Jonas Wilms

Reputation: 138257

You could build a set of obj2:

  const names = new Set(obj2.map(it => it.name));

Then mapping obj1 can be done in O(n):

  const result = obj1.map(({ name }) => ({ name, matched: names.has(name) }));

Upvotes: 1

Nithish
Nithish

Reputation: 5999

You can make use of Array.map and Array.some.

const obj1 = [{name:'Mark'},{name:'David'},{name:'Ellie'},{name:'Zuank'},{name:'Philip'},{name:'Den'}];

const obj2 = [{name:'Mark'},{name:'David'},{name:'Zuank'},{name:'Philip'}];


const getProcessedResult = (data1, data2) => {
  return data1.map(d1 => ({
    ...d1,
    "matched": data2.some(d2 => d2.name === d1.name)
  }));
}

console.log(getProcessedResult(obj1, obj2))
.as-console-wrapper {
  max-height: 100% !important;
}

Upvotes: 0

hgb123
hgb123

Reputation: 14891

You could do a map obj1 with some on obj2 to check if obj1 element is on obj2

const obj1 = [
  { name: "Mark" },
  { name: "David" },
  { name: "Ellie" },
  { name: "Zuank" },
  { name: "Philip" },
  { name: "Den" },
]

const obj2 = [
  { name: "Mark" },
  { name: "David" },
  { name: "Zuank" },
  { name: "Philip" },
]

const res = obj1.map((el1) => ({
  name: el1.name,
  match: obj2.some((el2) => el2.name === el1.name),
}))

console.log(res)

Upvotes: 2

Ele
Ele

Reputation: 33726

You can use the function map to build the desired output and the function some to check for a specific object by the property name.

const obj1 = [{name:"Mark"},            {name:"David"},            {name:"Ellie"},            {name:"Zuank"},            {name:"Philip"},            {name:"Den"},        ],
      obj2 = [{name:"Mark"},        {name:"David"},        {name:"Zuank"},        {name:"Philip"},    ],
      result = obj1.map(({name}) => ({name, matched: obj2.some(({name: name2}) => name2 === name)}));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 2

Related Questions