在Javascript

时间:2017-03-03 16:02:16

标签: javascript arrays object

我不太清楚如何清楚地表达我在这篇文章的标题中所做的事情,所以如果标题具有误导性或过于模糊,请原谅我。

我有一个从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);
    }

现在我可以拨打&#39; truncNotes&#39;在我的UI5 XML视图中的对象,它返回如下:

enter image description here

1 个答案:

答案 0 :(得分:1)

执行此操作的一种方法是首先按note_date对数组进行排序,然后创建一个新数组,并仅向其添加两个具有相同note_type_description值的对象。

以下是一个例子:

&#13;
&#13;
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);
&#13;
&#13;
&#13;

这不是最有效的方法,但它会向您介绍JavaScript数组函数,例如sortfilterforEach