2017-03-03 62 views
0

我不太清楚如何阐明我在这篇文章的标题中要做的事情,所以如果标题有误导性或者含糊不清,请原谅我。用Javascript处理对象数组

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

现在我可以称之为“truncNotes”对象在我UI5 XML视图,并返回为这样:

enter image description here

+2

[访问/进程(嵌套)对象,数组或JSON]的可能重复(http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Liam

+3

请发布您试过的代码 – user93

+1

您是什么意思最近的条目?此外,什么阻止你使用现有的数组来呈现你的输出,为什么你不能只使用你拥有的数组? – dave

回答

1

这样做将是由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阵列功能,如sortfilterforEach

+0

谢谢@Titus,我认为这是指向我需要去的方向。我不得不转换为常规功能,因为目前我没有奢侈品利用ES6。 –