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.

5
  • 5
    you need at least different keys ...? Commented Jan 29, 2019 at 13:16
  • You need different keys or call like let newobject={"1","2" } Commented Jan 29, 2019 at 13:17
  • what's wrong with this [{"key":"1"},{key:"2"}] object? Commented Jan 29, 2019 at 13:18
  • this is impossible unless you have different keys. using key for two entries in a map is a collision Commented Jan 29, 2019 at 13:19
  • what is a "dynamic object" for you exactly? Commented Jan 29, 2019 at 13:19

4 Answers 4

7

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);

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

2 Comments

I am using typescript and get this error Property 'assign' does not exist on type 'ObjectConstructor'.
There is some discussion about how to handle this issue here: stackoverflow.com/questions/35959372/…
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)

1 Comment

plus 1 for Spanish lesson :)
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);

3 Comments

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?
@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.
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 :)
0

Using ES5:

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

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

console.log(output);

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.