1

I had an array

[{0:{title:"test1"}},{1:{message1:"message1"}},{2:{message2:"message2"}}]

I want to get an Object in below format

{title:"test1",message1:"message1",message2:"message2"}

I am trying with below code but no luck

var rv = {};
for (var i = 0; i < messageArray.length; ++i)
if (messageArray[i] !== undefined) rv[i] = messageArray[i];`

It results me

{0: {title:"test1"}, 1: {message1:"message1"},2: {message2:"message2"}}
2
  • I find it hard to believe that what you've shown is actually the content of your array. Are you sure? It's very, very strange. Commented Apr 23, 2018 at 15:44
  • Yes thats my array :( Commented Apr 23, 2018 at 15:47

6 Answers 6

2

You can use reduce to summarize the data and Object.assign() to merge new properties into an object.

let arr = [{0:{title:"test1"}},{1:{message1:"message1"}},{2:{message2:"message2"}}];

let result = arr.reduce((c, v, i) => Object.assign(c, v[i]),{});

console.log(result);

If the key on your objects are not on sequence, you can use Object.values() to get the values.

let arr = [{0:{title:"test1"}},{1:{message1:"message1"}},{2:{message2:"message2"}}];

let result = arr.reduce((c, v, i) => Object.assign(c, Object.values(v)[0]), {});

console.log(result);

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

Comments

1

You can use .map() and .reduce():

let data = [
  {0:{title:"test1"}},
  {1:{message1:"message1"}},
  {2:{message2:"message2"}}
];

let result = data.map((c, i) => c[i])
                 .reduce((a, c) => Object.assign(a, c), {});

console.log(result);

Docs:

Comments

1

You can combine the items by spreading into Object.assign(). Then get the internal values via Object.values(), and merge them again by spreading into Object.assign():

const data = [{0:{title:"test1"}},{1:{message1:"message1"}},{2:{message2:"message2"}}];

const result = Object.assign(...Object.values(Object.assign({}, ...data)));

console.log(result);

Comments

0

You should make your array flat

const rv = [];
for (var i = 0; i < messageArray.length; ++i) {
   if (messageArray[i] !== undefined)
      rv.push(messageArray[i]);
}

Then can use Array.reduce, it should look like

rv.reduce((result, element) => {
   Object.keys(element).forEach((key) => {
      result[key] = element[key];
   });
   return result;
}, {});

Comments

0

You can iterate over each property, and use Object.assign to extend the current object with each property.

var object = {};
var input = [{0:{title:"test1"}},{1:{message1:"message1"}},{2:{message2:"message2"}}];

for (var i = 0; i < input.length; i++) {
    object = Object.assign(input[i][i], object);
}

console.log(object);

/*
{
  "message2": "message2",
  "message1": "message1",
  "title": "test1"
}
*/

Comments

0

The following code should give desired results without Object.assign :

  //Using map() and reduce()
  var arr = [{0:{title:"test1"}},{1:{message1:"message1"}},{2:{message2:"message2"}}];

  var result = arr.map(function(item,i){
    return item[i];
  })
  .reduce(function(acc,item){
    return {...acc,...item}
  },{})

  console.log(result); // {title: "test1", message1: "message1", message2: "message2"}

Hope this helps!

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.