1

I have an array of arrays with the following structure:

[
   [ {"key": someKey, value: "someValue"}, 
     {key: someOtherKey, value: "someOtherValue} ],
   [ {"key": someKey, value: "someValue"}, 
     {key: someOtherKey, value: "someOtherValue} ],
]

I want to transform this structure into a list of object with dynamic keys and values like such:

[{someKey: someValue}, {someOtherKey: someOtherValue}]

So far i have this code

      const serializedData = data.map(entry =>{
            console.log(entry)
            return entry.reduce(
                (obj, item) => {
                    return Object.assign(obj, { [item.key]: item.value })
                }) 
    })

But this only serializes the keys for the first entry (i have provided image examples below), can anybody help me to create the correct dataformat?

4 Answers 4

2

You could flatten the array and then map over it to get the values in each object and create a new object from those values using Object.fromEntries() function

const arr = [
   [ {"key": 'someKey1', value: "someValue1"}, {key: 'someOtherKey2', value: "someOtherValue2"} ],
   [ {"key": 'someKey3', value: "someValue3"}, {key: 'someOtherKey4', value: "someOtherValue4"} ],
];

const result = arr.flat().map(({key, value}) => Object.fromEntries([[key, value]]));

console.log(result);
.as-console-wrapper{min-height:100% !important; top: 0; }

You could also pass the result of Object.values() function to Object.fromEntries() function to create the object.

const arr = [
   [ {"key": 'someKey1', value: "someValue1"}, {key: 'someOtherKey2', value: "someOtherValue2"} ],
   [ {"key": 'someKey3', value: "someValue3"}, {key: 'someOtherKey4', value: "someOtherValue4"} ],
];

const result = arr.flat().map(obj => Object.fromEntries([Object.values(obj)]));

console.log(result);
.as-console-wrapper{min-height:100% !important; top: 0; }

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

Comments

2

You can use array#flat and array#map.

const arr = [ [{ "key": 'someKey1', value: "someValue1" }, { key: 'someOtherKey2', value: "someOtherValue2" } ], [{ "key": 'someKey3', value: "someValue3" }, { key: 'someOtherKey4', value: "someOtherValue4" } ], ],
      result = arr.flat().map(({key, value}) => ({[key]: value}));
console.log(result);
.as-console-wrapper{min-height:100% !important; top: 0; }

Comments

1

const data = [
  [{
      key: "k01",
      value: "v01"
    },
    {
      key: "k02",
      value: "v02"
    }
  ],
  [{
      key: "k11",
      value: "v11"
    },
    {
      key: "k12",
      value: "v12"
    }
  ],
];

const newData = data
  .flat()
  .reduce(
    (acc, {
      key,
      value
    }) =>
    Object.assign(acc, {
      [key]: value
    }), {}
  )

console.log(newData);

Comments

0

You can use Map to create the objects and then make objects from it

kvp=[
  [ {key: "someKey", value: "someValue"}, 
    {key: "someOtherKey", value: "someOtherValue"} ],
  [ {key: "someKey", value: "someValue"}, 
    {key: "someOtherKey", value: "someOtherValue"} ],
].flat()
      

result=[]
kvp.forEach(o=>{
  map=new Map([Object.values(o)])
  objx = Object.fromEntries(map)
  result.push(objx)
})
console.log(result)

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.