-1

I've this object called

const formValues = {...}

inside this object we have an array, this value must have one element or more,

formValues: { 
    locations: [...];
}

inside the array locations we have multiple values: strings and objects.

formValues: { 
    locations: [
        key1: string
        key2: string
        key3: object
        key4: string
        key5: object
        key6: string
        key7: object
    ];
}

How can i convert the key5 to an array ?

i already tried using the map of lodash, but it doesn't work because at some point it generates a double array in the key5 ( key5: [[...]] )

const newFormValues = map(formValues, item =>
    map(item, el => ({
      ...el,
      workHours: [el.workHours],
    })),
  );

 const data = {
    data: { locations: flatten(newFormValues) },
    section: 'locations',
    confirm,
  };

EXPECTED RESULT

The same object

formValues: { 
    locations: [
        key1: string
        key2: string
        key3: object
        key4: string
        key5: **array**
        key6: string
        key7: object
    ];
}

but the key5 must be an array instead of an object

Legeo
  • 784
  • 4
  • 20
  • 44

1 Answers1

1

You can try

Object.entries(the_object)

For example if the object is

var theObject = {a: 1, b: 2};
Object.entries(theObject) 

gives you the result [ ["a", 1], ["b", 2]]

Linh Nguyen
  • 925
  • 1
  • 10
  • 23