admin管理员组文章数量:1333442
Is there any nifty way to get values from an array of hashes:
var foo = {a: "aFirst", b: "bFirst", c: "cFirst"}; var boo = {a: "aSecond", b: "bSecond", c: "cSecond"}; var bar = {a: "aThird", b: "bThird", c: "cThird"}; var myArrOfHashes = [foo, boo, bar];
So I would expect something like:
myArrOfHashes.map(b) // => bFirst, aSecond, aThird
Is there any nifty way to get values from an array of hashes:
var foo = {a: "aFirst", b: "bFirst", c: "cFirst"}; var boo = {a: "aSecond", b: "bSecond", c: "cSecond"}; var bar = {a: "aThird", b: "bThird", c: "cThird"}; var myArrOfHashes = [foo, boo, bar];
So I would expect something like:
myArrOfHashes.map(b) // => bFirst, aSecond, aThird
Share
Improve this question
asked Jun 1, 2013 at 8:19
Jackie ChanJackie Chan
2,6626 gold badges36 silver badges71 bronze badges
1
-
1
sorry but this line isn't clear to me (
myArrOfHashes.map(b) // => bFirst, aSecond, aThird
) can you elaborate more plz. – yogi Commented Jun 1, 2013 at 8:24
3 Answers
Reset to default 4One easy way to do this - and many similar things - is with the Lo-Dash or Underscore libraries.
Here's an example from the Lo-Dash documentation:
var stooges = [
{ 'name': 'moe', 'age': 40 },
{ 'name': 'larry', 'age': 50 }
];
_.pluck(stooges, 'name');
// → ['moe', 'larry']
Even if you use a different approach for this particular problem, you should definitely check out these libraries. (They are very similar to each other; between the two I prefer Lo-Dash.)
Well, not quite like that, but you could try this:
myArrOfHashes.map(function(hash){ return hash.b; });
It's not 100% clear what you want, but try
var map = function(key) {
return function(value) {
return value[key];
};
};
console.log(myArrOfHashes.map(map('b')));
本文标签: arraysHow to get values by key in JavascriptStack Overflow
版权声明:本文标题:arrays - How to get values by key in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742354186a2459088.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论