So i have 2 arrays of objects , i want to get the name value from one and assign it to the label and value . This is what i am doing.
let newAreas = [{ label: '', value: '' }];
let areas = [{name: 'Haram', condition: true, counter: 5}, {name: 'Nasr City', condition: false, counter: 3}, {name: 'Faisl', condition: true, counter: 7}];
i want to get the each name and assign them to label and value so now i loop
areas.map(area => {
newAreas.map(val => {
val.label = area.name;
val.value = area.name;
});
});
console.log(newAreas);
but this only gets me the last value what am i doing wrong here ?
You only want to use map
once, and retrieve the returned value:
const areas = [ {name: 'Haram', condition: true, counter: 5}, {name: 'Nasr City', condition: false, counter: 3}, {name: 'Faisl', condition: true, counter: 7} ]; const newAreas = areas.map(({name}) => ({label: name, value: name})); console.log(newAreas);
This makes use of object destructuring .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.