0

I would like to transform the following array which is dynamic to an dynamic object

e.g.

let

array=[{"MyFirstValue":"1"},{MySecondvalue:"2"}]

I would like to convert it ti an dynamic object

like

let newobject={MyFirstValue:"1",MySecondvalue:"2" }

The dynamic object here is that depending upon the numbers of values in the array the newobject should be able to transform e.g.

if the array value changes =

array=[{"MyFirstValue":"1"},{MySecondvalue:"2"},{MyThirdValue:"2"}]

The newobject should transform to

newobject={MyFirstValue:"1",MySecondvalue:"2",MyThirdValue:"2" }

I tried using array.map and pushing the value but could not set the key property

It is like reducing the reducing the array to an object. I am not sure if i can use reduce.

user1339913
  • 1,017
  • 3
  • 15
  • 36

4 Answers4

6

You can accomplish this using Object.assign assuming your keys are unique.

const array = [{"key1":"1"}, {"key2":"2"}];

const newObject = Object.assign({}, ...array);

console.log(newObject);
Nick
  • 16,066
  • 3
  • 16
  • 32
  • I am using typescript and get this error Property 'assign' does not exist on type 'ObjectConstructor'. – user1339913 Jan 29 '19 at 13:29
  • There is some discussion about how to handle this issue here: https://stackoverflow.com/questions/35959372/property-assign-does-not-exist-on-type-objectconstructor – Nick Jan 29 '19 at 13:41
1

You need to have unique keys in your object. Also, you should not expect all your objects to have 100% unique keys. Instead of having {key: 1, key: 2}, why not have the property key be an array of values; {key: [1, 2]}. I would use Array.prototype.reduce to achieve this.

const array = [{"key":"1", other: 'FOO'},{key:"2", dynamic: 'BAR', other: 'BAZ'}]

const object = array.reduce((initial, value) => {
  Object.keys(value).forEach((key) => {
    if(initial[key]) initial[key].push(value[key])
    else initial[key] = [value[key]]
  })
  return initial;
}, {})

console.log(object)
Cory Kleiser
  • 1,969
  • 2
  • 13
  • 26
0

try

let newobject= array.reduce((x,y)=> ({...x,...y}), {});

let array=[{"MyFirstValue":"1"},{MySecondvalue:"2"}]

let newobject= array.reduce((x,y)=> ({...x,...y}), {});

console.log(newobject);
Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345
  • This way will introduce bugs if either of your objects have the same key (the second object's value will override the first's). Is it a good idea to make the assumption that both objects have completely unique keys? – Cory Kleiser Jan 29 '19 at 19:42
  • @CoryKleiser Uno: OP in first question version use two same keys, but he correct this and made unique keys. Secundo: OP doesn't describe what kind of result he expect in case when keys are not unique (e.g. may be if he will interested on that case then he want to get last value of non unique key in array... ) so this means that non-unique keys case is out of the scope of OP question. – Kamil Kiełczewski Jan 29 '19 at 19:50
  • Ok, that makes sense; still, I doubt OP wants to trivially throw away values based on the order of objects in the array. Spanish 101 lesson: *first* in Spanish is *primero* :) – Cory Kleiser Jan 29 '19 at 20:01
0

Using ES5:

var input = [{"MyFirstValue":"1"},{MySecondvalue:"2"},{MyThirdValue:"2"}];

var output = Object.assign.apply({}, input);

console.log(output);
Melchia
  • 22,578
  • 22
  • 103
  • 117