2017-03-03 10 views
0

私はこの投稿のタイトルで何をしようとしているのかを明確にする方法がわからないので、タイトルが誤解を招くかあまりにも曖昧であれば、私を許してください。JavaScriptのオブジェクトの配列を操作する

私はoData呼び出し(これはSAPUI5アプリケーション内にあります)から作成したオブジェクトの配列を持っています。実際のオブジェクトには3つ以上のキーと値のペアがありますが、この例を単純にするためにそれらを取り除きました。

[{ 
    "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)の各タイプの2つを含むオブジェクトの新しい配列(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ビューで「truncNotes」オブジェクトを呼び出すことができますし、そうと返されます。これを行うための

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して配列をソートしてから、作成することになります新しい配列を追加し、同じnote_type_description値を持つオブジェクトを2つだけ追加します。ここで

は一例です:

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);

これはそれを行うための最も効率的な方法ではありませんが、それはsortfilterforEach

のようなJavaScriptの配列の機能を紹介します
+0

ありがとうございます@ティタス、私はこれが私が行く必要がある方向に向いていると思います。私はこの時点でES6を贅沢に利用することはできないため、通常の機能に変換しなければなりませんでした。 –

関連する問題