admin管理员组

文章数量:1400007

I am trying to pare 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 })
        }
    })

I am trying to pare 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 })
        }
    })
Share Improve this question edited Aug 27, 2020 at 14:30 Saif asked Aug 27, 2020 at 14:24 SaifSaif 2851 gold badge6 silver badges26 bronze badges 4
  • Can you show what you have tried so far to solve this problem yourself? – Yousaf Commented Aug 27, 2020 at 14:26
  • can you please share what have you tried? – Shail_bee 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 }) } }) – Saif Commented Aug 27, 2020 at 14:30
  • @CodeReady Done. – Saif Commented Aug 27, 2020 at 14:31
Add a ment  | 

6 Answers 6

Reset to default 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)

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

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

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

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

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

本文标签: Comparing two arrays and filtering one array upon this in javascript and reactStack Overflow