3

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

["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.

3
  • yours is not a bad way... for what reason you are looking for another way? Commented Jul 8, 2015 at 20:11
  • What's wrong with forEach? Are you looking for some out-of-the box method that does this? Commented Jul 8, 2015 at 20:12
  • There is nothing wrong with forEach. I was just wondering if there was another predefined function to accomplish the same result. Commented Jul 8, 2015 at 20:18

4 Answers 4

4

Combine map(), values(), and unzip() into a wrapper:

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" ]
Sign up to request clarification or add additional context in comments.

1 Comment

+1 but be aware that _values() doesn't guarantee order, so it might not be usable depending on the code requirements.
3

Array.map() works well for this:

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

Comments

2

Looks like there is nothing shorter then two pluck's from 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>

Comments

2

The code you have is just fine, but if you're looking for something more "functional," there's always 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" ]

2 Comments

So the second parameter of reduce creates and initializes properties on the arguments of the first function?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.