Я хочу добавить массив объектов к определенной записи массива odata. Это JSON-модель в sapui5, и я хотел сделать oModel.getData()[j].setProperty(""), но это не так. Кто-нибудь может помочь?
У меня есть:
[{
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" }
]
И я хочу добавить новый элемент:
[{
description: "Ereignisgruppe"
key: "objectGroup"
value: "Aufgaben"
},{
description: "Ereignis"
key: "object"
value: "Aufgabe"
}]
Чтобы получить это:
[{
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"
}]
С наилучшими пожеланиями!
В вашем json отсутствует запятая за каждым атрибутом.
Затем вы можете добавить «masterAttributes», например:
oModel[0].masterAttributes = masterAttributes;
проверьте эту скрипку:
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.info("oModel after adding masterAttributes ");
console.info(oModel);
// 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.info(newData);
Вам просто нужно создать новое свойство и присвоить соответствующие значения
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];
}
Возможный дубликат Как я могу получить доступ и обработать вложенные объекты, массивы или JSON?