2017-03-03 61 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。 –