使用 JavaScript 和/或 D3.js 在对象数组中获取给定键的所有值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22355466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting all the values for a given key, in an array of objects, using JavaScript and/or D3.js
提问by user3226861
I'm building a dashboard that uses D3.js for charts. I have a large array of objects. Each object has 32 key value pairs, with the same keys. Doesn't anyone know a good way to get all the values for a given key?
我正在构建一个使用 D3.js 作为图表的仪表板。我有大量的对象。每个对象有 32 个键值对,具有相同的键。没有人知道获取给定键的所有值的好方法吗?
EDIT: As soon as I asked the question a simple function came to me. Also thought maybe a function already existed that I wasn't finding.
编辑:我一问这个问题,一个简单的功能就来找我了。还认为可能已经存在一个我没有找到的功能。
function getValues(data, key){
var values = [];
data.forEach(function(d){
var v = d[key];
if(!d3.set(values).has(v)){
values.push(v);
}
})
return values;
}
采纳答案by Daniel F
I have to create a lot of dashboards using D3 as well. Another option for you which I utilize a lot is underscore.js. It saves me time, and for my needs helps condense code. There is a function called 'pluck' in underscore, which does exactly what you asked for. For an array of objects, you can declare a key and it will return all values.
我还必须使用 D3 创建很多仪表板。我经常使用的另一个选择是 underscore.js。它节省了我的时间,并且根据我的需要帮助压缩代码。下划线中有一个名为“pluck”的函数,它完全符合您的要求。对于对象数组,您可以声明一个键,它将返回所有值。
Example:
例子:
var data = [{name: 'dan', value: 40}, {name: 'ryan', value: 50}];
var getKeys = _.pluck(data, 'name');
=> ["dan", "ryan"]
回答by Joshua Romero
If you're already using d3, take a look at Mike Bostock's "Underscore Equivalents" gist: https://gist.github.com/mbostock/3934356
如果您已经在使用 d3,请查看 Mike Bostock 的“Underscore Equivalents”要点:https: //gist.github.com/mbostock/3934356
So
所以
data.map(function(d) { return d[key]; });
will get you all the values. If you only want unique values, use
会给你所有的价值。如果您只想要唯一值,请使用
d3.set(data.map(function(d) { return d[key]; })).values());
回答by Learning stats by example
Use d3.keys()
:
使用d3.keys()
:
d3.keys(data).filter(function(key) { return key })
回答by Royi Namir
Assuming you mean values
as simple primitive values -
假设你的意思values
是简单的原始值 -
You can use this altered code of myne (which was originally used to flatten object actually) - to find all the values for specific key (recursive) :
您可以使用 myne 的这个修改过的代码(它最初实际上是用来扁平化对象的) - 找到特定键(递归)的所有值:
Example : for a verycomplex object , let's find all the values for a given key named "a"
示例:对于一个非常复杂的对象,让我们找到名为“a”的给定键的所有值
var data ={a:5,g: [{"a":1,"b":[4,5,6,{a:55},[33, new Date()]]},{"c":2},{"a":3}]};
var g=[];
if (!Object.keys) { //not all browsers support it
Object.keys = function (obj) {
var keys = [],
k;
for (k in obj) {
if (Object.prototype.hasOwnProperty.call(obj, k)) {
keys.push(k);
}
}
return keys;
};
}
function actualType(o)
{
return Object.prototype.toString.apply(o);
}
var arr=actualType([]);
var obj=actualType({});
function work(a, val)
{
if (actualType(a) == obj || actualType(a) == arr)
{
for (var j = 0; j < Object.keys(a).length; j++)
{
if (Object.keys(a)[j] == val) g.push(a[Object.keys(a)[j]]);
else
work(a[Object.keys(a)[j]], val);
}
}
}
work(data,'a') //'a' is the value we're searching
console.log(g) //[5, 1, 55, 3]