2017-10-11 5 views
0

長さが> 1になり、ちょうど大きくなる可能性がある配列があります。オブジェクトの配列内のすべてのオブジェクトの2つの要素を引き出して新しいものを作成するには

親配列要素の値のうち2つだけを抽出し、新しい配列に入れる必要があります。

ここでベース・アレイの一例です:

{ 
    "strains":[ 
      { 
       "id": 150, 
       "name": "Animal Cookies (Phat Panda)", 
       "label": "Animal Cookies (Phat Panda)", 
       "thcpct": 14, 
       "cbdpct": 19, 
       "ttlpct": 33, 
       "type": "Hybrid SD", 
       "subtype": "Sativa Dominant", 
       "bitactive": 1, 
       "useradded": "jjones", 
       "dateadded": "4/12/2017" 
      }, 
      { 
       "id": 110, 
       "name": "Blue Dream (Pioneer)", 
       "label": "Blue Dream (Pioneer)", 
       "thcpct": 24, 
       "cbdpct": 0, 
       "ttlpct": 24, 
       "type": "Sativa", 
       "subtype": "None", 
       "bitactive": 1, 
       "useradded": "jjones", 
       "dateadded": "3/27/2017" 
      }, 
      { 
       "id": 30, 
       "name": "Candyland", 
       "label": "Candyland", 
       "thcpct": 25, 
       "cbdpct": 75, 
       "ttlpct": 100, 
       "type": "Hybrid SD", 
       "subtype": "Sativa Dominant", 
       "bitactive": 1, 
       "useradded": "jjones", 
       "dateadded": "1/1/2017" 
      }, 
      { 
       "id": 130, 
       "name": "Dragon OG (Avitas)", 
       "label": "Dragon OG (Avitas)", 
       "thcpct": 26, 
       "cbdpct": 18, 
       "ttlpct": 44, 
       "type": "Hybrid SD", 
       "subtype": "Sativa Dominant", 
       "bitactive": 1, 
       "useradded": "jjones", 
       "dateadded": "4/10/2017" 
      } 
    ] 
} 

、これは私はそれが抽出後に見えるようにしたいものです:私はこれを行うにしようとしました

{ 
    "strains":[ 
      { 
       "id": 150, 
       "label": "Animal Cookies (Phat Panda)", 
      }, 
      { 
       "id": 110, 
       "label": "Blue Dream (Pioneer)", 
      }, 
      { 
       "id": 30, 
       "label": "Candyland", 
      }, 
      { 
       "id": 130, 
       "label": "Dragon OG (Avitas)", 
      } 
    ] 
} 

方法ですプッシュでは、1つの配列が別の配列と同じですが、プッシュは関数ではありません。あるいは、 'id'が未定義でコードが停止します。

これは簡単です。配列要素を別の配列要素に割り当てることは難しくありませんが、なぜこの場合はどうして違うのですか?

私はここにLODASHを使用して試してみた:

svc.appFuncs = { 
    //https://stackoverflow.com/questions/32452014/change-key-name-in-javascript-object 
    //This replaces a STATIC key value for now, but can probably 
    //replace any value 
      fix_key: function (key, keyname) { 
      return key.replace(key, keyname); 
      } 
} 

I:ここで

//This is the call to the FIX_KEY function in appservice 
    _.object(
    _.map(
     _.keys(allStrains.data), 
     ctrlMain.appSvc.appFuncs.fix_key(/^name/, 'label')), 
     _.values(allStrains.data) 
); 

はappservice内の関数です

は、私がここで試した何のためのサンプルコードを持っていますコメント内のStack Overflow質問からlodashコードを変更しました。

UPDATE 1:

Andrjez、私はこの仕事をする必要があるどのように最終結果を見ると、どこで問題が発生していてください。

   * Angular Dropdown Multi-select 
      * 
      * For STRAINS to select what will be in the room selected 
      * 
      */ 
      $scope.example8model = []; 

      //Get the data from LOCALSTORAGE 
      var getResults; 
      **//I GET THE RESULTS from LOCAL STORAGE HERE** 
      getResults = storageLocalService.getItemJSON("allstrains"); 
      **//CHECK the strains from LOCAL STORAGE** 
      console.log("Strains: ", getResults.strains); //FAILS HERE!!! 
      //getResults.strains is UNDEFINED but getResults HOLDS the JSON 
      //EXACTLY how you made it in your example. I just changed 
      //**obj** to **getResults**. 



      var result = { 
       //NEXT LINE FAILS: getResults.strains.map is UNDEFINED!  
       strains: getResults.strains.map(function (item) { 
        return { 
         id: item.id, 
         label: item.label 
        }; 
       }) 
      }; 

      console.log("All extracted Strains: ", result); 

      $scope.example8model = result; 

      //$scope.example8data = [ 
      // {id: 1, label: "David"}, 
      // {id: 2, label: "Jhon"}, 
      // {id: 3, label: "Danny"} 
      //]; 
      $scope.example8settings = { 
       checkBoxes: true 
      }; 

ここで問題があります:LOCALのデータSTORAGEには、前後に二重引用符がありません。しかし...

これは私がDEV CONSOLEで得るものです:

お知らせ、先頭または末尾のために添加されていないDBLのQUOTE: enter image description here enter image description here

それから私がダウンして次のレベルにクリックしたとき最後にgetResults.strains.map()...

enter image description here

、:、二重引用符は、アクセスしようとしてめちゃくちゃに追加されますので、だから、エラー...

enter image description here

を結果として、どこが間違っているんですか?

注:これは私が達成しようとしているものです:そのようMULTI-SELECT

答えて

1

使用Array.prototype.map

var result = { 
    strains: obj.strains.map(function (item) { 
     return { 
      id: item.id, 
      label: item.label 
     }; 
    }) 
}; 

結果を表示するには、以下のスニペットを試してみてください。

const result = { strains: obj.strains.map(({id , label}) => ({id, label})) }; 
:あなたはES6の機能を使用する場合は

var obj = { 
 
    "strains":[ 
 
      { 
 
       "id": 150, 
 
       "name": "Animal Cookies (Phat Panda)", 
 
       "label": "Animal Cookies (Phat Panda)", 
 
       "thcpct": 14, 
 
       "cbdpct": 19, 
 
       "ttlpct": 33, 
 
       "type": "Hybrid SD", 
 
       "subtype": "Sativa Dominant", 
 
       "bitactive": 1, 
 
       "useradded": "jjones", 
 
       "dateadded": "4/12/2017" 
 
      }, 
 
      { 
 
       "id": 110, 
 
       "name": "Blue Dream (Pioneer)", 
 
       "label": "Blue Dream (Pioneer)", 
 
       "thcpct": 24, 
 
       "cbdpct": 0, 
 
       "ttlpct": 24, 
 
       "type": "Sativa", 
 
       "subtype": "None", 
 
       "bitactive": 1, 
 
       "useradded": "jjones", 
 
       "dateadded": "3/27/2017" 
 
      }, 
 
      { 
 
       "id": 30, 
 
       "name": "Candyland", 
 
       "label": "Candyland", 
 
       "thcpct": 25, 
 
       "cbdpct": 75, 
 
       "ttlpct": 100, 
 
       "type": "Hybrid SD", 
 
       "subtype": "Sativa Dominant", 
 
       "bitactive": 1, 
 
       "useradded": "jjones", 
 
       "dateadded": "1/1/2017" 
 
      }, 
 
      { 
 
       "id": 130, 
 
       "name": "Dragon OG (Avitas)", 
 
       "label": "Dragon OG (Avitas)", 
 
       "thcpct": 26, 
 
       "cbdpct": 18, 
 
       "ttlpct": 44, 
 
       "type": "Hybrid SD", 
 
       "subtype": "Sativa Dominant", 
 
       "bitactive": 1, 
 
       "useradded": "jjones", 
 
       "dateadded": "4/10/2017" 
 
      } 
 
    ] 
 
}; 
 

 
var result = { 
 
    strains: obj.strains.map(function (item) { 
 
     return { 
 
      id: item.id, 
 
      label: item.label 
 
     }; 
 
    }) 
 
}; 
 

 
console.log(result);

それだけで株以上だと、あなたは、各サブアレイに同じロジックを適用する場合は

+0

ありがとうAndrzej、これは "最高の"答えでした! –

+0

Andrzej、これは動作しませんが、それはあなたのコードではありません、私はJSONを引っ張っています...詳細については、更新1を参照してください!レビューをお願いします....ありがとうございます –

0
for (n of strains) { 
    n = n.filter(function(elem,index){ 
     return (index == "id" || index == "label") 
    }); 
} 

は何か?

+0

使用を示唆他のコメントのようなマップがありますより良いオプション – Shard

1
obj.strains = obj.strains.map(x => ({ id: x.id, label: x.label })); 

let filtered = Object.entries(obj) 
    .filter(([k, arr]) => Array.isArray(arr)) 
    .reduce((acc, [k, arr]) => { 
    acc[k] = arr.map(x => ({ id: x.id, label: x.label })); 
    return acc; 
    }, {}); 
+0

obj.strains = obj.strains.map(x =>({id:x.id、label:x.label})); – Shard

+0

はい、ES6に準拠していないブラウザはどうですか?バベルを使う? –

+0

@Shardマップは副作用がなく、新しい配列を作成します。 –

1

私の解決策:

var test = 'label' 
var results = []; 
var response = strains.forEach(obj => { 
    if (obj[test]) { 
     results = [ 
      ...results, 
      { 
       test: obj[test] 
      } 
     ]; 
    } 
}); 

console.log('matches', results); 
//output: matches, [{test: "Animal Cookies (Phat Panda)"}, {test: "Blue Dream (Pioneer)"}, {test: "Candyland"}, {test: "Dragon OG (Avitas)"}] 
関連する問題