1

I have a json array. I need to bring this:

[
  {"id": ["1"],
   "title": ["hello"],
   "start": ["2016-05-20"],
   "end": ["2016-05-25"],
  }
]

to this:

[
  {"id": "1",
   "title: "hello",
   "start": "2016-05-20",
   "end": "2016-05-25",
  }
]

How to do that?

Pranav C Balan
  • 110,383
  • 23
  • 155
  • 178

3 Answers3

2

You could loop with Array#forEach() and assign all properties with the first element.

var array = [{ "id": ["1"], "title": ["hello"], "start": ["2016-05-20"], "end": ["2016-05-25"], }];

array.forEach(function (a) {
    Object.keys(a).forEach(function (k) {
        a[k] = a[k][0];
    });
});

console.log(array);
Nina Scholz
  • 351,820
  • 24
  • 303
  • 358
1

Use forEach and Object.keys()

var data = [{
  "id": ["1"],
  "title": ["hello"],
  "start": ["2016-05-20"],
  "end": ["2016-05-25"],
}];
data.forEach(function(obj) {
  Object.keys(obj).forEach(function(v) {
    obj[v] = obj[v][0];
  });
});
console.log(data);
Pranav C Balan
  • 110,383
  • 23
  • 155
  • 178
1

We can do using .map and for-in loop

var test = [
  {"id": ["1"],
   "title": ["hello"],
   "start": ["2016-05-20"],
   "end": ["2016-05-25"],
  }
]
test.map(function(x){
  for(var key in x){
   x[key] = x[key].join('')
  }
  return x;
});
Jagdish Idhate
  • 6,997
  • 8
  • 31
  • 49