長さが> 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:
それから私がダウンして次のレベルにクリックしたとき最後にgetResults.strains.map()...
、:、二重引用符は、アクセスしようとしてめちゃくちゃに追加されますので、だから、エラー...
を結果として、どこが間違っているんですか?
注:これは私が達成しようとしているものです:そのようMULTI-SELECT
ありがとうAndrzej、これは "最高の"答えでした! –
Andrzej、これは動作しませんが、それはあなたのコードではありません、私はJSONを引っ張っています...詳細については、更新1を参照してください!レビューをお願いします....ありがとうございます –