我不太确定如何在这篇文章的标题中阐明我想要做什么,所以如果标题具有误导性或过于含糊,请原谅我。
我有一个通过 oData 调用创建的对象数组(这是在 SAPUI5 应用程序中)。实际对象具有三个以上的键/值对,但我将它们删除以保持此示例简单。
[{
"note_type_description": "General",
"note_date": "/Date(872850505000)/",
"note_text": "THIS IS A SUBSIDUARY OF THAT."
},
{
"note_type_description": "General",
"note_date": "/Date(873072000000)/",
"note_text": "Say What Now?"
},
{
"note_type_description": "General",
"note_date": "/Date(891388800000)/",
"note_text": "Say Who Now?"
},
{
"note_type_description": "General",
"note_date": "/Date(891993600000)/",
"note_text": "Say When Now?"
},
{
"note_type_description": "Interaction",
"note_date": "/Date(909014400000)/",
"note_text": "Say How Now?"
},
{
"note_type_description": "Interaction",
"note_date": "/Date(906422400000)/",
"note_text": "Say Valentine Now?"
},
{
"note_type_description": "Interaction",
"note_date": "/Date(1485907200000)/",
"note_text": "The latest interaction."
},
{
"note_type_description": "Company Information",
"note_date": "/Date(1477958400000)/",
"note_text": "Some information about Person"
},
{
"note_type_description": "Company Information",
"note_date": "/Date(1483228800000)/",
"note_text": "Are they private or public?"
},
{
"note_type_description": "Company Information",
"note_date": "/Date(1485907200000)/",
"note_text": "Hope this is enough information!"
},
{
"note_type_description": "Relationship Strategy",
"note_date": "/Date(1485993600000)/",
"note_text": "Good!"
},
{
"note_type_description": "Relationship Strategy",
"note_date": "/Date(1487116800000)/",
"note_text": "Better!"
},
{
"note_type_description": "Relationship Strategy",
"note_date": "/Date(1488412800000)/",
"note_text": "Best!"
},
{
"note_type_description": "Relationship Strategy",
"note_date": "/Date(1490918400000)/",
"note_text": "Superb!"
}]
我想迭代对象,并创建一个新的对象数组,其中包含最近条目 (note_date) 的每种类型 (note_type_description) 中的两个,以便我可以在 UI 中呈现它。
我对 JS 有点陌生,所以我主要不清楚我将使用什么数组方法来完成此任务。我认为这将从 Array.map() 开始并从那里开始。任何援助将不胜感激!我将继续努力并发布我在此过程中的任何更新!
** 更新 **
我使用了 @Titus 的示例,这是经过一些修改后的样子(从箭头函数切换到常规函数):
var oArr = [{
"note_type_description": "General",
"note_date": "/Date(872850505000)/",
"note_text": "THIS IS A SUBSIDUARY OF THAT."
}, {
"note_type_description": "General",
"note_date": "/Date(873072000000)/",
"note_text": "Say What Now?"
}, {
"note_type_description": "General",
"note_date": "/Date(891388800000)/",
"note_text": "Say Who Now?"
}, {
"note_type_description": "General",
"note_date": "/Date(891993600000)/",
"note_text": "Say When Now?"
}, {
"note_type_description": "Interaction",
"note_date": "/Date(909014400000)/",
"note_text": "Say How Now?"
}, {
"note_type_description": "Interaction",
"note_date": "/Date(906422400000)/",
"note_text": "Say Valentine Now?"
}, {
"note_type_description": "Interaction",
"note_date": "/Date(1485907200000)/",
"note_text": "The latest interaction."
}, {
"note_type_description": "Company Information",
"note_date": "/Date(1477958400000)/",
"note_text": "Some information about Person"
}, {
"note_type_description": "Company Information",
"note_date": "/Date(1483228800000)/",
"note_text": "Are they private or public?"
}, {
"note_type_description": "Company Information",
"note_date": "/Date(1485907200000)/",
"note_text": "Hope this is enough information!"
}, {
"note_type_description": "Relationship Strategy",
"note_date": "/Date(1485993600000)/",
"note_text": "Good!"
}, {
"note_type_description": "Relationship Strategy",
"note_date": "/Date(1487116800000)/",
"note_text": "Better!"
}, {
"note_type_description": "Relationship Strategy",
"note_date": "/Date(1488412800000)/",
"note_text": "Best!"
}, {
"note_type_description": "Relationship Strategy",
"note_date": "/Date(1490918400000)/",
"note_text": "Superb!"
}];
var sortedArr = oArr.sort(function(a, b) {
b.note_date.match(/\d+/)[0] - a.note_date.match(/\d+/)[0]
});
var toRender = [];
sortedArr.forEach(function(v) {
if (toRender.filter(function(vv) {
return v.note_type_description == vv.note_type_description
}).length < 2) {
toRender.push(v);
}
});
toRender.forEach(function(oKey) {
console.log(oKey.note_type_description + " | " + oKey.note_text);
});
***** 更新#2 *****
为了完成这个并提供上下文,我最终得到的是:
_setNotes: function(oResponse) {
if (typeof oResponse.results !== "undefined") {
var aAllNotes = oResponse.results;
var aTruncNotes = [];
var sortedNotes = aAllNotes.sort(function(a, b) {
a = new Date(a.note_date);
b = new Date(b.note_date);
return a>b ? -1 : a<b ? 1 : 0;
});
sortedNotes.forEach(function(v) {
if (aTruncNotes.filter(function(vv) {
return v.note_type_description === vv.note_type_description;
}).length < 2) {
aTruncNotes.push(v);
}
});
}
this.getView().getModel("view").setProperty("/allNotes", aAllNotes);
this.getView().getModel("view").setProperty("/truncNotes", aTruncNotes);
}
现在我可以在 UI5 XML View 中调用“truncNotes”对象,它会按如下方式返回:
最佳答案
实现此目的的一种方法是首先按 note_date
对数组进行排序,然后创建一个新数组并仅向其中添加 2 个具有相同 note_type_description
值的对象.
这是一个例子:
var arr = [{
"note_type_description": "General",
"note_date": "/Date(872850505000)/",
"note_text": "THIS IS A SUBSIDUARY OF THAT."
},
{
"note_type_description": "General",
"note_date": "/Date(873072000000)/",
"note_text": "Say What Now?"
},
{
"note_type_description": "General",
"note_date": "/Date(891388800000)/",
"note_text": "Say Who Now?"
},
{
"note_type_description": "General",
"note_date": "/Date(891993600000)/",
"note_text": "Say When Now?"
},
{
"note_type_description": "Interaction",
"note_date": "/Date(909014400000)/",
"note_text": "Say How Now?"
},
{
"note_type_description": "Interaction",
"note_date": "/Date(906422400000)/",
"note_text": "Say Valentine Now?"
},
{
"note_type_description": "Interaction",
"note_date": "/Date(1485907200000)/",
"note_text": "The latest interaction."
},
{
"note_type_description": "Company Information",
"note_date": "/Date(1477958400000)/",
"note_text": "Some information about Person"
},
{
"note_type_description": "Company Information",
"note_date": "/Date(1483228800000)/",
"note_text": "Are they private or public?"
},
{
"note_type_description": "Company Information",
"note_date": "/Date(1485907200000)/",
"note_text": "Hope this is enough information!"
},
{
"note_type_description": "Relationship Strategy",
"note_date": "/Date(1485993600000)/",
"note_text": "Good!"
},
{
"note_type_description": "Relationship Strategy",
"note_date": "/Date(1487116800000)/",
"note_text": "Better!"
},
{
"note_type_description": "Relationship Strategy",
"note_date": "/Date(1488412800000)/",
"note_text": "Best!"
},
{
"note_type_description": "Relationship Strategy",
"note_date": "/Date(1490918400000)/",
"note_text": "Superb!"
}];
var sortedArr = arr.sort((a, b) => b.note_date.match(/\d+/)[0] - a.note_date.match(/\d+/)[0]);
var toRender = [];
sortedArr.forEach(v => {
if(toRender.filter(vv => v.note_type_description == vv.note_type_description).length < 2){
toRender.push(v);
}
});
console.log(toRender);
这不是最有效的方法,但它将向您介绍 JavaScript 数组函数,例如 sort
、filter
和 forEach
关于javascript - 在 Javascript 中操作对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42583420/