how to change value of the object to an object contains key:value key:value to all models array if there any way please share it with me
const hello = [
{
brand: "Acura",
models: [
"2.2CL",
"2.3CL",
"3.0CL",
"3.2CL",
"ILX",
"Integra",
"Legend",
"MDX",
"NSX",
"RDX",
"3.5 RL",
"RL",
"RSX",
"SLX",
"2.5TL",
"3.2TL",
"TL",
"TSX",
"Vigor",
"ZDX"
]
},
{
brand: "Alfa Romeo",
models: [
"164",
"8C Competizione",
"GTV-6",
"Milano",
"Spider"
]
},
{
brand: "AMC",
models: [
"Alliance",
"Concord",
"Eagle",
"Encore",
"Spirit"
]
},
and make it like this for all models {value:"value", label:"value"},
const hello = [
{
brand: "Acura",
models: [
{value:"2.2CL", label:"2.2CL"},
{value:"2.3CL", label:"2.3CL"},
.
and make it like this for all models {value:"value", label:"value"},and make it like this for all models {value:"value", label:"value"},and make it like this for all models {value:"value", label:"value"},
CodePudding user response:
Here is what you could do :
const hello = [{
brand: "Acura",
models: [
"2.2CL",
"2.3CL",
"3.0CL",
"3.2CL",
"ILX",
"Integra",
"Legend",
"MDX",
"NSX",
"RDX",
"3.5 RL",
"RL",
"RSX",
"SLX",
"2.5TL",
"3.2TL",
"TL",
"TSX",
"Vigor",
"ZDX"
]
},
{
brand: "Alfa Romeo",
models: [
"164",
"8C Competizione",
"GTV-6",
"Milano",
"Spider"
]
},
{
brand: "AMC",
models: [
"Alliance",
"Concord",
"Eagle",
"Encore",
"Spirit"
]
},
];
const result = hello.map(item => {
return {
...item,
models: item.models.map(val => {
return {
value: val,
label: val
}
})
}
});
console.log(result)
CodePudding user response:
Maybe this is not the best answer, but the map and reduce methods is what you are looking for
const result = hello.map((item) => {
const models = item.models.reduce((acum, curr) => {
acum.push({
value: curr,
label: curr,
});
return acum;
}, []);
return {
brand: item.brand,
models,
};
});