I have two variables which is an array and array of object, I want to add the value of first variable(distance) to second variable(list)
The following works fine, but I want to know if there's any other method to get some result.
let distance = [100,200,300]
let list = [ {"city" : "paris"} , {"city" : "london"} , { "city" : "barcelona" }]
for(let i = 0; i < distance.length;i++){
let listDistance = list.map(el => {
return Object.assign({}, el, {distance:distance[i++]})
return el
});
console.log(listDistance)
}
// output [ {city : paris , distance : 100 } , {city : london , distance : 200 } , { city : barcelona , distance : 300 }]
map's callback