The problem of object combination in js Array

there are two sets of data
let a = [

{
    brandName: "aaa",
    brandId: "75",
},
{
    brandName: "bbb",
    brandId: "12",
},
{
    brandName: "ccc",
    brandId: "16",
}

]

let b = [

]
{
    voteid: 12,
    totolnumber: 110
},
{
    voteid: 16,
    totolnumber: 220
},
{
    voteid: 100,
    totolnumber: 20
},
{
    voteid: 75,
    totolnumber: 20
}

]

how can you match two id and form a third array?
let c = [

{
    brandName: "aaa",
    brandId: "75",
    totolnumber: 20
},
{
    brandName: "bbb",
    brandId: "12",
    totolnumber: 110
},
{
    brandName: "ccc",
    brandId: "16",
    totolnumber: 220
}

]

Sep.10,2021

how about two loops?
var arr = []
a.map(e=>{
  b.map(o=>{
    if(e.brandId == o.voteid){
      let obj ={
        brandId:e.brandId,
        totolnumber:o.totolnumber,
        brandName:e.brandName
      }
      arr.push(obj)

    }
  })
})
console.log(arr)
//   


clipboard.png


let a = [
  {
    brandName: 'aaa',
    brandId: '75',
  },
  {
    brandName: 'bbb',
    brandId: '12',
  },
  {
    brandName: 'ccc',
    brandId: '16',
  },
];

let b = [
  {
    voteid: 12,
    totolnumber: 110,
  },
  {
    voteid: 16,
    totolnumber: 220,
  },
  {
    voteid: 100,
    totolnumber: 20,
  },
  {
    voteid: 75,
    totolnumber: 20,
  },
];

let c = a.map(item => {
  const d = b.find(({voteid}) => voteid == item.brandId);
  return {
    ...item,
    totolnumber: d ? d.totolnumber : 0,
  };
});

console.log(c);

this kind of simple question, it is recommended to master the basics of js first

Menu