简体   繁体   中英

How to data to Array using vanilla Javascript

I need object to convert to array and push to array. Depending on what is chosen, I get it like this ->

{
 firstUnit: {label: 'caca', value: 70}
 secondUnit: {label: 'Parent 2', value: 9}
} 

Or sometimes i got only

{
 firstUnit: {label: 'caca', value: 70} 
} 

or

{ 
 secondUnit: {label: 'Parent 2', value: 9}
} 

What I need?

I need to convert this

{
 firstUnit: {label: 'caca', value: 70}
 secondUnit: {label: 'Parent 2', value: 9}
}  

or

{
 firstUnit: {label: 'caca', value: 70} 
} 

TO

 [ {label: 'caca', value: 70} , {label: 'Parent 2', value: 9}]

or if only one object

 [{label: 'caca', value: 70}]

What I am try?

let formVal = [formValues];
let newVal = [formVal?.map((values) => values)]

This is no work property.

Object.values will work here.

The Object.values() method returns an array of a given object's own enumerable property values

 const data = { firstUnit: { label: 'caca', value: 70 }, secondUnit: { label: 'Parent 2', value: 9 } }; const out = Object.values(data); console.log(out);

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM