Map the key value of one object to the corresponding attribute in another object array according to the key of the object

problem description

var obj ={
  apple: 1,
  banana:2,
  li:30
};
var arr = [
  {id:"1",text:"",key:"apple", value:""},
  {id:"2",text:"", key:"li",value:""},
  {id:"3",text:"", key:"banana", value:""}
];

// :
var newArr = [
  {id:"1",text:"",key:"apple", value:"1"},
  {id:"2",text:"", key:"li",value:"2"},
  {id:"3",text:"", key:"banana", value:"30"}
];

how is it implemented?

Jul.13,2022

isn't the pear 30.

var obj ={
  apple: 1,
  banana:2,
  li:30
}
var arr = [
  {id:'1',text:'',key:'apple', value:''},
  {id:'2',text:'', key:'li',value:''},
  {id:'3',text:'', key:'banana', value:''}
]

const newArr = arr.map(o => {
  o.value = obj[o.key]
  return o
})

console.log(newArr)

clipboard.png

if it helps you, please upvote or adopt


var newArr = arr.map(item=>{
    let newObj = JSON.parse(JSON.stringify(item))
    let value = obj[ newObj[key] ]
    newObj.value = value ? value : ''
    return newObj
})

var data = arr.map (function (item) {

)
item.value = obj[item.key];
return item

});

Menu