简体   繁体   English

如何将一个对象数组拆分成多个普通数组,其中每个数组都由一个属性的值填充?

[英]How to split an array of objects into multiple normal arrays where each array is populated by one property's values?

I have an array of objects as follows: 我有一个对象数组如下:

var GOM = [{name:"Kuroko",ability:"misdirection"},
           {name:"Kise",ability:"perfect copy"},
           {name: "Akashi", ability: "emperor's eye"}];

Is it possible to split it into 2 arrays using some predefined function in lodash or native javascript(not forEach). 是否可以使用lodash中的某些预定义函数或本机javascript(不是forEach)将其拆分为2个数组。

["Kuroko","Kise","Akashi"] and ["misdirection","perfect copy","emperor's eye"]

I know I can just do this: 我知道我可以这样做:

var names = [];
var abilities = [];
GOM.forEach(function(member){
  names.push(member.name);
  abilities.push(member.ability);
});

but I am looking for another way. 但我正在寻找另一种方式。

Combine map() , values() , and unzip() into a wrapper: map()values()unzip()组合成包装器:

var wrapper = _(GOM).map(_.values).unzip();

Then it's easy to grab the names: 然后很容易获取名称:

wrapper.first()
// → [ "Kuroko", "Kise", "Akashi" ]

And abilities: 和能力:

wrapper.last()
// → [ "misdirection", "perfect copy", "emperor's eye" ]

Array.map() works well for this: Array.map()适用于此:

 var GOM = [{name:"Kuroko",ability:"misdirection"},{name:"Kise",ability:"perfect copy"},{name: "Akashi", ability: "emperor's eye"}]; var names = GOM.map(function(item) { return item.name; }); var abilities = GOM.map(function(item) { return item.ability; }); alert(names + '\\n' + abilities); 

The code you have is just fine, but if you're looking for something more "functional," there's always reduce : 你拥有的代码很好,但是如果你正在寻找更具“功能性”的东西,那么总会reduce

var GOM = [ { name: "Kuroko", ability: "misdirection" },
            { name: "Kise", ability: "perfect copy" },
            { name: "Akashi", ability: "emperor's eye" } ];

var result = GOM.reduce(function(memo, member) {
  memo.names.push(member.name);
  memo.abilities.push(member.ability);
  return memo;
}, { names: [], abilities: [] });

console.log(result.names);
// => [ "Kuroko", "Kise", "Akashi" ]

console.log(result.abilities);
// => [ "misdirection", "perfect copy", "emperor's eye" ]

Looks like there is nothing shorter then two pluck's from lodash: 看起来没有什么比Lodash的两个更短的了:

 var data = [ {name:"Kuroko",ability:"misdirection"}, {name:"Kise",ability:"perfect copy"}, {name: "Akashi", ability: "emperor's eye"} ]; var names = _.pluck(data, 'name'); var abilities = _.pluck(data, 'ability'); alert(JSON.stringify(names, null, 4)); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.0/lodash.js"></script> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据其中一个值中的数据将对象拆分为多个对象[数组中的唯一值] - How to split an object into multiple objects based on data in one of it's values[unique values in an array] 如何将一个包含数组的 object 拆分为多个对象 - How to split one object with an array in it to multiple objects 如何使用javascript通过对象属性将数组拆分为数组? - How to split an array into arrays by an objects property using javascript? 如何将具有两个属性的对象数组减少为两个 arrays,每个属性一个? - How to reduce an array of objects with two properties into two arrays, one for each property? 从一组对象中,将属性提取到多个数组中(每个属性一个)? - From an array of objects, extract properties into multiple arrays (one per property)? 将对象中的数组属性扩展为使用数组值填充的多个对象 - Expand Array property in Object to Multiple Objects populated w/ Array value 如何根据对象的值将对象数组拆分为另一个数组? - How to split an Array of Objects into another Arrays based on their values? 如何将对象数组转换为仅包含一个属性值的数组? - How do I convert an array of objects to an array including only one property's values? 将多个对象数组合并为一个对象数组 - merge multiple arrays of objects into one array of objects 如何获取对象数组并根据属性值创建数组? - How to take an array of objects and create arrays based on property values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM