0

I want to add a array of objects to a specific entry of an odata array. It is a JSON-Model in sapui5 and I wanted to do oModel.getData()[j].setProperty(""), but this does not. Can anyone help?

I have:

    [{
            attributes: (5) [{…}, {…}, {…}, {…}, {…}]
            creationDate: "2019-05-20T09:14:16.622Z"
            deleted: false
            eventDate: "20.05.19"            
            message: "Aufgabe xyz"
            object: "Aufgabe"            
    },
{
            attributes: (5) [{…}, {…}, {…}, {…}, {…}]
            creationDate: "2019-05-20T09:14:16.622Z"
            deleted: false
            eventDate: "21.05.19"            
            message: "Aufgabe"
            object: "Aufgabe"    }
]

And I want to add a new Item:

[{
        description: "Ereignisgruppe"
        key: "objectGroup"
        value: "Aufgaben"
    },{
        description: "Ereignis"
        key: "object"
        value: "Aufgabe"
   }]

To get this:

 [{
                attributes: (5) [{…}, {…}, {…}, {…}, {…}]
                creationDate: "2019-05-20T09:14:16.622Z"
                deleted: false
                eventDate: "20.05.19"            
                message: "Aufgabe xyz"
                object: "Aufgabe",
                masterAttributes: (2) [{…}, {…}]        <-------------- new  
        },
    {
                attributes: (5) [{…}, {…}, {…}, {…}, {…}]
                creationDate: "2019-05-20T09:14:16.622Z"
                deleted: false
                eventDate: "21.05.19"            
                message: "Aufgabe"
                object: "Aufgabe"    
   }]

Best regards!

Galadriel
  • 359
  • 5
  • 20
  • 1
    Possible duplicate of [How can I access and process nested objects, arrays or JSON?](https://stackoverflow.com/questions/11922383/how-can-i-access-and-process-nested-objects-arrays-or-json) – str May 30 '19 at 19:10

3 Answers3

1

Your json is missing the comma behind every attribute.

You can then add the 'masterAttributes' like:

oModel[0].masterAttributes = masterAttributes;

check this fiddle:

     var oModel= [{
                attributes: [],
                creationDate: "2019-05-20T09:14:16.622Z",
                deleted: false,
                eventDate: "20.05.19"  ,          
                message: "Aufgabe xyz",
                object: "Aufgabe"       
        },
    {
                attributes: [],
                creationDate: "2019-05-20T09:14:16.622Z",
                deleted: false,
                eventDate: "21.05.19",        
                message: "Aufgabe",
                object: "Aufgabe"    }
    ];
    
    var masterAttributes = [{
        description: "Ereignisgruppe",
        key: "objectGroup",
        value: "Aufgaben"
    },{
        description: "Ereignis",
        key: "object",
        value: "Aufgabe",
   }];
    
   // add the other array
   oModel[0].masterAttributes = masterAttributes;
   
   console.log("oModel after adding masterAttributes ");
   console.log(oModel);
codeteq
  • 1,502
  • 7
  • 13
1
    // es6
let oModel= [{
                        attributes: [],
                        creationDate: "2019-05-20T09:14:16.622Z",
                        deleted: false,
                        eventDate: "20.05.19"  ,          
                        message: "Aufgabe xyz",
                        object: "Aufgabe"       
                },
            {
                        attributes: [],
                        creationDate: "2019-05-20T09:14:16.622Z",
                        deleted: false,
                        eventDate: "21.05.19",        
                        message: "Aufgabe",
                        object: "Aufgabe"    }
            ];

            let masterAttributes = [{
                description: "Ereignisgruppe",
                key: "objectGroup",
                value: "Aufgaben"
            },{
                description: "Ereignis",
                key: "object",
                value: "Aufgabe",
           }];
           let newData = { ...oModel, [0]: { ...oModel[0], masterAttributes: masterAttributes}};
           console.log(newData);
ericrj
  • 11
  • 3
1

You just need to create a new property and assign the corresponding values

var oData= [{
    attributes: [],
    creationDate: "2019-05-20T09:14:16.622Z",
    deleted: false,
    eventDate: "20.05.19"  ,          
    message: "Aufgabe xyz",
    object: "Aufgabe"       
}, {
    attributes: [],
    creationDate: "2019-05-20T09:14:16.622Z",
    deleted: false,
    eventDate: "21.05.19",        
    message: "Aufgabe",
    object: "Aufgabe"    
   }    
];
var oMergeData = [{
    description: "Ereignisgruppe",
    key: "objectGroup",
    value: "Aufgaben"
   },{
    description: "Ereignis",
    key: "object",
    value: "Aufgabe",
   }
];
//Loop main object and add new property with corresponding values
for( var i in oData) { 
  var oNewData = oData[i];
  oNewData['masterAttributes '] = oMergeData[i];
}
Inizio
  • 2,226
  • 15
  • 18