0

I have an json array as follows:

Maindata=[
  {"name":"string1"},
  {"name":"string2"},
  {"name":"string3"}
 ];

what I need is an array of following type:

data=[
  {
   "name":"string1",
   "name":"string2",
   "name":"string3"
  }
 ];

can anybody help me with some methods to obtain required json from original array. (note: maindata is json array formed dynamically thats why its structure is like that)

Thanks in advance

6
  • 2
    I don't think what you want as output is a valid object Commented Feb 28, 2017 at 10:06
  • 1
    why is data not an object? why is there an array? Commented Feb 28, 2017 at 10:09
  • 2
    Your second JSON is not valid JSON since object cannot have duplicated name. Commented Feb 28, 2017 at 10:24
  • as I said Maindata is obtained dynamically creating html elemnts. like clicking a button so to add textboxes. So key of each element remains same. value String differs for each of them Commented Feb 28, 2017 at 11:57
  • please look for my updated question and json Commented Feb 28, 2017 at 11:59

3 Answers 3

2

You could use Object.assign and spread the array elements.

var array = [{ name1: "string1" }, { name2: "string2" }, { name3: "string3" }],
    object = Object.assign({}, ...array);
    
console.log(object);

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

Comments

0

With reduce, you can do like following

    var Maindata = [{
      "name1": "string"
    }, {
      "name2": "string"
    }, {
      "name3": "string"
    }];

    var finalObj = Maindata.reduce((acc, cur) => {
      Object.assign(acc, cur);
      return acc;
    }, {})

    console.log(finalObj);

2 Comments

its working if name and string are unique every time as your answer says. but in my case "name" stays same at each element so its returning only last element. can you update your answer. thank you
You cannot have two properties of a single object with the same name.
0

You can use Array.forEach or Array.reduce to iterate though the items of the Maindata object and for each item you can iterate through its keys(using Object.keys) and group the data into a new structure.(See the below snippet)

Solution using Array.forEach

var Maindata=[
  {"name1":"string1"},
  {"name2":"string2"},
  {"name3":"string3"}
 ];
 var result = {};
 var newMaindata=[];
 Maindata.forEach(function(el){
 Object.keys(el).forEach(function(key){
 result[key]=el[key];
 });
 });
 newMaindata.push(result);
 
 console.log(newMaindata);

Solution using Array.reduce

var Maindata = [{
  "name1": "string1"
}, {
  "name2": "string2"
}, {
  "name3": "string3"
}];
var result ;
var newMaindata = [];
result = Maindata.reduce(function(acc,el) {
  Object.keys(el).forEach(function(key) {
    acc[key] = el[key];
  });
  return acc;
},{});
newMaindata.push(result);

console.log(newMaindata);

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.