我想将对象数组添加到odata数组的特定条目。 它是sapui5中的JSON模型,我想做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"
}]
最诚挚的问候!
答案 0 :(得分:1)
您的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.log("oModel after adding masterAttributes ");
console.log(oModel);
答案 1 :(得分: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);
答案 2 :(得分:1)
您只需要创建一个新属性并分配相应的值
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];
}