admin管理员组

文章数量:1225618

I have a Json object coming from a backend server called Data like this:

[{
  "categoryName": "Google",
  "id": "58591d2b7672d99910497bec",
  "clientId": "585808f6737f6aad1985eab2"
}, {
  "categoryName": "Microsoft",
  "id": "58591d3d7672d99910497bee",
  "clientId": "585808f6737f6aad1985eab2"
}, "3", {
  "categoryName": "Yahoo",
  "id": "58591d4c7672d99910497bef",
  "clientId": "585808f6737f6aad1985eab2"
}, {
  "categoryName": "Msn",
  "id": "585d25f6ae4b2ecb056bc514",
  "clientId": "585808f6737f6aad1985eab2"
}]

and I would like to add a column (property?) to each row like this:

[{
  "categoryName": "Google",
  "id": "58591d2b7672d99910497bec",
  "clientId": "585808f6737f6aad1985eab2",
  "new column": ""
}, {
  "categoryName": "Microsoft",
  "id": "58591d3d7672d99910497bee",
  "clientId": "585808f6737f6aad1985eab2",
  "new column": ""
}, "3", {
  "categoryName": "Yahoo",
  "id": "58591d4c7672d99910497bef",
  "clientId": "585808f6737f6aad1985eab2",
  "new column": ""
}, {
  "categoryName": "Msn",
  "id": "585d25f6ae4b2ecb056bc514",
  "clientId": "585808f6737f6aad1985eab2",
  "new column": ""
}]

Does anyone how I can do this?

Thanks in advance.

I have a Json object coming from a backend server called Data like this:

[{
  "categoryName": "Google",
  "id": "58591d2b7672d99910497bec",
  "clientId": "585808f6737f6aad1985eab2"
}, {
  "categoryName": "Microsoft",
  "id": "58591d3d7672d99910497bee",
  "clientId": "585808f6737f6aad1985eab2"
}, "3", {
  "categoryName": "Yahoo",
  "id": "58591d4c7672d99910497bef",
  "clientId": "585808f6737f6aad1985eab2"
}, {
  "categoryName": "Msn",
  "id": "585d25f6ae4b2ecb056bc514",
  "clientId": "585808f6737f6aad1985eab2"
}]

and I would like to add a column (property?) to each row like this:

[{
  "categoryName": "Google",
  "id": "58591d2b7672d99910497bec",
  "clientId": "585808f6737f6aad1985eab2",
  "new column": ""
}, {
  "categoryName": "Microsoft",
  "id": "58591d3d7672d99910497bee",
  "clientId": "585808f6737f6aad1985eab2",
  "new column": ""
}, "3", {
  "categoryName": "Yahoo",
  "id": "58591d4c7672d99910497bef",
  "clientId": "585808f6737f6aad1985eab2",
  "new column": ""
}, {
  "categoryName": "Msn",
  "id": "585d25f6ae4b2ecb056bc514",
  "clientId": "585808f6737f6aad1985eab2",
  "new column": ""
}]

Does anyone how I can do this?

Thanks in advance.

Share Improve this question edited Jan 19, 2017 at 13:06 Philippe Corrèges asked Dec 28, 2016 at 14:20 Philippe CorrègesPhilippe Corrèges 6892 gold badges12 silver badges36 bronze badges 3
  • you could use javascript's map function to add the new property to each object. but why would you do it if it's going to be an empty string anyway? – toskv Commented Dec 28, 2016 at 14:23
  • 5 why is there a "3" suddenly? – Jecfish Commented Dec 28, 2016 at 14:24
  • It is not going to stay empty, of course. – Philippe Corrèges Commented Dec 28, 2016 at 15:12
Add a comment  | 

2 Answers 2

Reset to default 10

Try this:

data.forEach(function(e){
  if (typeof e === "object" ){
    e["new column"] = ""
  }
});

Needs that typeof check because of that weird 3 in the middle.

You can do this:

const arr = data.map(x => Object.assign({}, data, { "new column": "" }))

本文标签: javascriptHow to add a column to items in an arrayStack Overflow