Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change array in javascript into simpler object

I have a simple JSON with an array that contains further objects, etc. like this:

languagePack:
[
  {
    'key': 'Username',
    'value': 'Benutzername',
    'group': 'default'
  },
  {
    'key': 'Password',
    'value': 'Passwort',
    'group': 'default'
  }
]

But what I really want is an object like this:

languagePack: 
{
    'Username': 'Benutzername',
    'Password': 'Passwort'
}

So, I want to reduce the array to simple key-value-pairs that are inside an array or even an object (keys are unique). Does anyone have an idea how to reduce this with some of these cool array functions? I only came up with something like an for each and building the object "by hand" property for property, but I remember there were some cool things for array like 'reduce', the spread operator (...), map, every, some, etc.

I tried it with something like:

var temp = this.languagePack.map(([key, value]) => ({key,value}))
console.log(temp)

But that only got me an error message TypeError: Invalid attempt to destructure non-iterable instance

Edit: All three answers are working perfectly fine. Thanks.

like image 935
Marcel Grüger Avatar asked Nov 15 '19 08:11

Marcel Grüger


People also ask

Can we convert array to object in JavaScript?

To convert an array to an object, use the reduce() method to iterate over the array, passing it an object as the initial value. On each iteration, assign a new key-value pair to the accumulated object and return the result. Copied!

How do you transform an array of objects?

To convert an array of objects to a Map , call the map() method on the array and on each iteration return an array containing the key and value. Then pass the array of key-value pairs to the Map() constructor to create the Map object.

How do you shorten an array in JavaScript?

In JavaScript, there are two ways of truncating an array. One of them is using length property and the other one is using splice() method.


2 Answers

Basically you need to use forEach instead of map function and then you can build that object to whatever key, value pair you want to keep.

Try this, it will solve your problem.

var temp = {};

this.languagePack.forEach(({key,value}) => {
    temp[key] = value
})

console.log(temp)

Note: Here we are not using map because we want to return object not an array, so, we can use reduce function here to do so, but I thought this would be simple and easy to understand what we want and what are we doing here.

like image 110
ma_dev_15 Avatar answered Oct 15 '22 23:10

ma_dev_15


You can use the javascript reduce function to create an empty object and put each key and value in it.

const data = [
  {
    'key': 'Username',
    'value': 'Benutzername',
    'group': 'default'
  },
  {
    'key': 'Password',
    'value': 'Passwort',
    'group': 'default'
  }
];

const newData = data.reduce((acc, row) => {
  acc[row.key] = row.value;
  return acc;
}, {});

console.log(newData);

Edit : Nice suggest of Donny Verduijn. You can use es6 destructuring to write the function shorter.

const newData = data.reduce((acc, { key, value }) => ({ ...acc, [key]: value }), {});
like image 40
Maxime Girou Avatar answered Oct 15 '22 23:10

Maxime Girou