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?

1

3 Answers 3

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

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

Comments

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

Comments

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

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.