'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.
Solution 1:[1]
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);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
