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

});

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b37287-2c07a.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b37287-2c07a.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?