我有一个数组,其长度可以在任何时候大于1并且只是越来越大。如何取出对象数组中的每个对象的两个元素,并创建一个新对象
我只需要提取父数组元素的两个值并将它们放入一个新数组中。
这里的基本阵列的例子:
{
"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)",
}
]
}
我试图做到这一点的方法是与推,一个数组等于另一个,仍然我得到推不是一个函数,或'ID'是未定义的,代码停止。
这是简单的东西,并分配一个数组元素到另一个并不困难,但为什么这种情况如此不同?
我对我在这里尝试了示例代码:
我在这里使用LODASH尝试:
//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服务功能:
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);
}
}
我在注释中修改堆栈溢出问题的lodash代码。
更新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
};
这里的问题:在本地数据存储没有双引号或前导或尾随。但是...
这是我得到的,开发者控制台:
然后当我点击到下一个级别,双引号被添加从而搞乱试图访问:getResults.strains.map()...
最后,造成错误...
所以,我要去哪里错了?
注意:这是我想要实现:MULTI-SELECT
谢谢Andrzej,这是“最好”的答案! –
Andrzej,这是行不通的,但它不是你的代码,这是我在JSON中拉的方式...查看更新1的细节!请检阅....谢谢 –