Smokey Dawson
Smokey Dawson

Reputation: 9230

convert an array of objects to a single object with certain value pairs

Im aware there are many questions on how to turn an array into an object I know you can do something like this.. object = {...arr}; but my question is more specific

say I have an array of objects like so..

[
    {
       dataType: something,
       dataValue: book
    },
    {
       dataType: food,
       dataValue: brocolli
    },
    {
       dataType: object,
       dataValue: chair
    },
]

and I want to create an object that looks like this..

{
   something: book,
   food: brocolli,
   object: chair
}

I tried to do something like this...

arr.forEach(item => {
   newarray.push({arr.dataType: arr.dataValue});
})
newObject = {...newarray}

but that didnt work.. any help would be appreciated!

Upvotes: 0

Views: 62

Answers (6)

Matt Way
Matt Way

Reputation: 33161

As an alternative to reduce, you can use Object.assign() and Array.map(). This is very similar to your original attempt, except for using object assign. I prefer this method, because the spread is only performed once, after the mapping has been created. My gut tells me this is more performant than reduce, but I haven't tested.

var arr = [{
    dataType: 'something',
    dataValue: 'book'
  },
  {
    dataType: 'food',
    dataValue: 'brocolli'
  },
  {
    dataType: 'object',
    dataValue: 'chair'
  },
]

var obj = Object.assign({}, ...arr.map(({ dataType, dataValue }) => ({ [dataType]: dataValue })))

console.log(obj);

Upvotes: 0

acdcjunior
acdcjunior

Reputation: 135772

Some Array.prototype.reduce(), destructuring and spread do the job:

var a = [
    {
       dataType: 'something',
       dataValue: 'book'
    },
    {
       dataType: 'food',
       dataValue: 'brocolli'
    },
    {
       dataType: 'object',
       dataValue: 'chair'
    },
];

// ES6
var new6 = a.reduce((o, {dataType, dataValue}) => ({...o, [dataType]: dataValue}), {});
console.log(new6);

//ES5
var new5 = a.reduce(function(o, i) {
  var prop = {}; prop[i.dataType] = i.dataValue;
  return Object.assign(o, prop); 
}, {});
console.log(new5);

//ES3.1 (lol)
var new31 = {}, i;
for (i = 0; i < a.length; i++) {
  new31[a[i].dataType] = a[i].dataValue;
}
console.log(new31);

Upvotes: 2

Ryan Yuan
Ryan Yuan

Reputation: 2566

You probably want to use newarray[item.dataType] = item.dataValue; to set to set those key-value pairs into the newarray.

var arr = [
        {
           dataType: 'something',
           dataValue: 'book'
        },
        {
           dataType: 'food',
           dataValue: 'brocolli'
        },
        {
           dataType: 'object',
           dataValue: 'chair'
        },
    ];
    var newarray = {};
    arr.forEach(item => {
        newarray[item.dataType] = item.dataValue;
    });
    newObject = {...newarray};

    console.log(newObject);

Upvotes: 1

Eddie
Eddie

Reputation: 26844

You can use reduce

var arr = [{
    dataType: 'something',
    dataValue: 'book'
  },
  {
    dataType: 'food',
    dataValue: 'brocolli'
  },
  {
    dataType: 'object',
    dataValue: 'chair'
  },
]

var obj = arr.reduce((c, {dataType,dataValue}) => Object.assign(c, {[dataType]: dataValue}), {});

console.log(obj);

Upvotes: 4

Ele
Ele

Reputation: 33726

You can use the function reduce.

var array = [    {       dataType: 'something',       dataValue: 'book'    },    {       dataType: 'food',       dataValue: 'brocolli'    },    {       dataType: 'object',       dataValue: 'chair'    },];

var result = array.reduce((a, {dataType, dataValue}) => {
  a[dataType] = dataValue;
  return a;
}, {});

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

Using Spread Syntax and computed property names.

var array = [    {       dataType: 'something',       dataValue: 'book'    },    {       dataType: 'food',       dataValue: 'brocolli'    },    {       dataType: 'object',       dataValue: 'chair'    },];

var result = array.reduce((a, {dataType, dataValue}) => ({...a, [dataType]: dataValue}), {});

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

Upvotes: 3

Vasyl Moskalov
Vasyl Moskalov

Reputation: 4630

Like this one?

arr.reduce((acc,val)=>{acc[val.dataType]=val.dataValue;return acc;},{})

Upvotes: 1

Related Questions