-1

I have this data structure

[
    {photos: [{name:'abc'}]},
    {photos: [{name:'xyz'}]},
]

How can I add name property as photo's sibling like this

[
    {photos: [{name:'abc'}], name:'abc'}
    {photos: [{name:'xyz'}], name: 'xyz'},
]
Alan Jenshen
  • 2,949
  • 9
  • 20
  • 33

2 Answers2

1

Something like that:

    var src = [
        {photos: [{name:'abc'}]},
        {photos: [{name:'xyz'}]},
    ]

    var dest = src.map(function (item) {
        return { photos: item.photos, name : item.photos[0].name} 
    })
norekhov
  • 3,043
  • 22
  • 38
1

Something like this?

x = [
    {photos: [{name:'abc'}]},
    {photos: [{name:'xyz'}]},
]

x[0].name = "abc"

console.log(x)

OP:- [
    {photos: [{name:'abc'}], name:'abc'}
    {photos: [{name:'xyz'}]},
]

Since this is an example I am just posting a way to add the name attribute. What's happening here is that since x is an array of json objects, it is simple to add attributes to the json object via x[0]

Sagar
  • 467
  • 4
  • 15