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
2 Answers
Reset to default 10Try 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
版权声明:本文标题:javascript - How to add a column to items in an array? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739404423a2161769.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论