我有这个数据结构
[
{photos: [{name:'abc'}]},
{photos: [{name:'xyz'}]},
]
如何添加名称属性作为照片的兄弟像这样
[
{photos: [{name:'abc'}], name:'abc'}
{photos: [{name:'xyz'}], name: 'xyz'},
]
答案 0 :(得分:1)
类似的东西:
var src = [
{photos: [{name:'abc'}]},
{photos: [{name:'xyz'}]},
]
var dest = src.map(function (item) {
return { photos: item.photos, name : item.photos[0].name}
})
答案 1 :(得分:1)
这样的东西?
x = [
{photos: [{name:'abc'}]},
{photos: [{name:'xyz'}]},
]
x[0].name = "abc"
console.log(x)
OP:- [
{photos: [{name:'abc'}], name:'abc'}
{photos: [{name:'xyz'}]},
]
由于这是一个例子,我只是发布了一种添加name属性的方法。 这里发生的是因为x是json对象的数组,所以通过x [0]
向json对象添加属性很简单