2

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 })
        }
    })
4
  • Can you show what you have tried so far to solve this problem yourself? Commented Aug 27, 2020 at 14:26
  • can you please share what have you tried? Commented Aug 27, 2020 at 14:26
  • 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 }) } }) Commented Aug 27, 2020 at 14:30
  • @CodeReady Done. Commented Aug 27, 2020 at 14:31

6 Answers 6

2

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)

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

Comments

2

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; }

Comments

1

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) }));

Comments

0

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;
}

Comments

0

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; }

Comments

0

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);

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.