2017-04-11 3 views
-1

が、それはきれいで、宣言的な方法で、得ることが可能である私はユニークサブJSON JSONの

[ 
    { 
    "id": 32837732, 
    "composer": { 
     "id": 536, 
     "name": "Francis Poulenc" 
    }, 
    "title": "Of Thee I Sing: Overture (radio version)" 
    }, 
    { 
    "id": 32837735, 
    "composer": { 
     "id": 245, 
     "name": "George Gershwin" 
    }, 
    "title": "Concerto in F : I. Allegro" 
    }, 
    { 
    "id": 32837739, 
    "composer": { 
     "id": 245, 
     "name": "George Gershwin" 
    }, 
    "title": "Concerto in F : II. Adagio" 
    } 
] 

のようなJSONオブジェクトから始めましょう、JSON

{ 
'composer': [ 
       { 
       'id': '536', 
       'name': 'Francis Poulenc' 
       }, 
       { 
       'id': '245', 
       'name': 'George Gershwin' 
       }, 
     ] 
} 

があるように各作曲家の固有のサブ値(IDと名前)?

+0

これ読めない、...コード形式で –

答えて

0

var arr= [ 
 
    { 
 
    "id": 32837732, 
 
    "composer": { 
 
     "id": 536, 
 
     "name": "Francis Poulenc" 
 
    }, 
 
    "title": "Of Thee I Sing: Overture (radio version)" 
 
    }, 
 
    { 
 
    "id": 32837735, 
 
    "composer": { 
 
     "id": 245, 
 
     "name": "George Gershwin" 
 
    }, 
 
    "title": "Concerto in F : I. Allegro" 
 
    }, 
 
    { 
 
    "id": 32837739, 
 
    "composer": { 
 
     "id": 245, 
 
     "name": "George Gershwin" 
 
    }, 
 
    "title": "Concerto in F : II. Adagio" 
 
    } 
 
]; 
 

 
var composers=[]; 
 
arr.forEach(function(arrElem){ 
 
    if(arrElem.composer && !composers.some(function(comp) { return comp.id === arrElem.composer.id && comp.name === arrElem.composer.name })) 
 
      composers.push(arrElem.composer); 
 
}); 
 
alert(JSON.stringify(composers));

+0

おかげでそれを入れて、あなたのコードスニペットは、ここで完璧に動作... は、残念ながら、私は、私はC#で働いていると言うことを忘れていました。.. もう一度お手伝いください! –