javascript - How to split an array of objects into multiple normal arrays where each array is populated by one property's values? -
i have array of objects follows:
var gom = [{name:"kuroko",ability:"misdirection"}, {name:"kise",ability:"perfect copy"}, {name: "akashi", ability: "emperor's eye"}];
is possible split 2 arrays using predefined function in lodash or native javascript(not foreach).
["kuroko","kise","akashi"] , ["misdirection","perfect copy","emperor's eye"]
i know can this:
var names = []; var abilities = []; gom.foreach(function(member){ names.push(member.name); abilities.push(member.ability); });
but looking way.
looks there nothing shorter 2 pluck's 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
Post a Comment