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
  • 113,687
  • 23
  • 165
  • 188

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
  • 376,160
  • 25
  • 347
  • 392
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
  • 113,687
  • 23
  • 165
  • 188
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
  • 7,513
  • 9
  • 35
  • 51