output: Array(5) [ "Samsung", "Iphone", "Nokia", "Xiomi", "Blackberry" ]
another output:
Array(5) [ Object { id: "Samsung", label: "Samsung" },Object { id: "Iphone", label: "Iphone"},
Object { id: "Nokia", label: "Nokia"},Object { id: "Xiomi", label: "Xiomi"},Object { id: "Blackberry", label: "Blackberry"} ]
I want my 1st output to get converted into the type of 2nd output. I am trying to do this:
mobilePhones = [
{
label: Object.keys(this.state.details.phones),
id: Object.keys(this.state.details.phones,
}
]
but getting empty mobilePhones. my state is of type:
state = {
details:{
phones: {},
}
}
I am converting phones object into array so that I can iterate it over const mobilePhones and then again want it to convert to the object type.Its getting confusing for me. Can anyone please help me with this.
const output = [ "Samsung", "Iphone", "Nokia", "Xiomi", "Blackberry" ] const brands = output.map(brand => ({id: brand, label: brand})); console.log(brands);
Use array.map
for this.
let array = [ "Samsung", "Iphone", "Nokia", "Xiomi", "Blackberry" ] let result = array.map( el => { return {id: el, label: el}}) console.log(result);
You can use map function to get the result of second array.
const type2 = type1.map((item) => {
return {
id: item,
label: item
}
});
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.