[英]Using a variable in an JSON query
I am trying to write a function that can take a field name as an argument and return an array of corresponding values from a bit of JSON.我正在尝试编写一个函数,该函数可以将字段名称作为参数并从一些 JSON 返回相应值的数组。
Example object:示例对象:
var myObject = [
{"x": 10, "y": 10},
{"x": 20, "y": 10},
{"x": 20, "y": 20},
{"x": 10, "y": 20}
];
My function looks something like this:我的函数看起来像这样:
function getValues(desiredValue) {
var values = [];
for (i = 0; i < myObject.length; i++) {
values[i] = myObject[i].desiredValue;
}
return values;
}
getValues(x);
Ideally, I would have the argument x
passed to the getValues
which, instead of looking for a field name called desiredValue
would look for a field name called x
.理想情况下,我会将参数
x
传递给getValues
,而不是查找名为desiredValue
的字段名称,而是查找名为x
的字段名称。
The returned array should look like this:返回的数组应如下所示:
[10,20,20,10]
As the problem with this code is obvious, how can I get the desired result?由于这段代码的问题很明显,我怎样才能得到想要的结果?
Also, I am trying to avoid unnecessary dependencies, so please don't give me any JQuery unless absolutely necessary.另外,我试图避免不必要的依赖,所以除非绝对必要,否则请不要给我任何 JQuery。
You can use map()
to return desired result.您可以使用
map()
返回所需的结果。
var myObject = [ {"x": 10, "y": 10}, {"x": 20, "y": 10}, {"x": 20, "y": 20}, {"x": 10, "y": 20} ]; function getValues(desiredValue) { return myObject.map(e => e[desiredValue]); } console.log(getValues('x'))
You actually need to parse the given JSON string (not the array that you have given here) by using JSON.parse().您实际上需要使用 JSON.parse() 解析给定的 JSON 字符串(不是您在此处提供的数组)。 See: http://jsbin.com/kevoqe/edit?js,console
参见: http : //jsbin.com/kevoqe/edit?js,console
a simple utility一个简单的实用程序
//also accepts a path like "foo.bar.baz"
//returns undefined if path can't be resolved
function fetch(path){
var keys = path.split(".");
return function( target ){
for(var t = target, i = 0; i < keys.length; t = t[ keys[ i++ ] ])
if(t == null) return void 0;
return t;
}
}
and it's usage它的用法
var myObject = [
{"x": 10, "y": 10},
{"x": 20, "y": 10},
{"x": 20, "y": 20},
{"x": 10, "y": 20}
];
var result = myObject.map( fetch("y") );
this version is a bit more flexible than one hardwired with Array.map()
because it can easily be composed with other functions.这个版本比用
Array.map()
硬连线的版本更灵活,因为它可以很容易地与其他函数组合。
Although, especially in this particular case, this already is a little bit of overkill.虽然,特别是在这种特殊情况下,这已经有点矫枉过正了。 here you can easily write:
在这里你可以很容易地写:
var result = myObject.map(pt => pt.y);
you can't get any shorter and simpler.你不能变得更短和更简单。 Or if for some reason the property really is dynamic, you'll have some variable containing it:
或者,如果由于某种原因该属性确实是动态的,您将有一些包含它的变量:
var dynamicPropertyName = "y";
//...
var result = myObject.map(pt => pt[ dynamicPropertyName ]);
Use array map method to do manipulation in an array of objects.使用数组映射方法对对象数组进行操作。
Try this code :试试这个代码:
var myObject = [
{"x": 10, "y": 10},
{"x": 20, "y": 10},
{"x": 20, "y": 20},
{"x": 10, "y": 20}
];
var output = getValues("x");
console.log(output);
function getValues(desiredValue) {
return myObject.map(function(item) {
return item[desiredValue];
});
}
Output :输出 :
Working fiddle : https://jsfiddle.net/ffyjyzjb/工作小提琴: https : //jsfiddle.net/ffyjyzjb/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.