0

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.

1
  • Use const newVal = Object.entries(formValues); might help you. Commented Mar 2, 2022 at 1:10

1 Answer 1

3

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);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.