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,除非绝对必要。
4 个解决方案
#1
1
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'))
#2
0
You actually need to parse the given JSON string (not the array that you have given here) by using JSON.parse(). See: http://jsbin.com/kevoqe/edit?js,console
实际上,您需要使用JSON.parse()解析给定的JSON字符串(不是这里给出的数组)。见:http://jsbin.com/kevoqe/edit?js控制台
#3
0
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 ]);
#4
0
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/
#1
1
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'))
#2
0
You actually need to parse the given JSON string (not the array that you have given here) by using JSON.parse(). See: http://jsbin.com/kevoqe/edit?js,console
实际上,您需要使用JSON.parse()解析给定的JSON字符串(不是这里给出的数组)。见:http://jsbin.com/kevoqe/edit?js控制台
#3
0
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 ]);
#4
0
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/