2017-10-11 346 views
0

我有一个数组,其长度可以在任何时候大于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 
      }; 

这里的问题:在本地数据存储没有双引号或前导或尾随。但是...

这是我得到的,开发者控制台:

通知,没有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 
     }; 
    }) 
}; 

尝试下面的代码片段看到的结果:

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

如果您希望使用ES6特点:

const result = { strains: obj.strains.map(({id , label}) => ({id, label})) }; 
+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兼容浏览器呢?使用Babel? –

+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)"}] 
相关问题