javascript 从 json 数组中提取特定键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23775190/
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
Pluck specific keys from json array
提问by tomalex
I'm trying to pluck some key-values from json array and form another array, below is the sample json structure. I got one solution now. Just want to know what are the other methods to do the same.
我试图从 json 数组中提取一些键值并形成另一个数组,下面是示例 json 结构。我现在有了一个解决方案。只是想知道还有什么其他方法可以做到这一点。
Is there anyway to reject keys from array of objects.
无论如何拒绝来自对象数组的键。
Input
输入
var a = [{
id: 1,
name: "First Name",
description: ""
},{
id: 2,
name: "Second Name",
description: ""
}]
Output
输出
[{
id: 1,
name: "First Name"
},{
id: 2,
name: "Second Name"
}]
One Solution
一个解决方案
var arr = [];
_.each(a,function(key,value){
arr.push(_.pick(key,'name','id'));
});
UPDATED
更新
Second Solution
第二种解决方案
_.map(a, function(o) { return _.pick(o, 'name','id'); });
采纳答案by Michael Geary
I would change one thing in your first solution, the parameter names in the _.each()
callback. In fact you only need one parameter here, just like the second version:
我会在您的第一个解决方案中更改一件事,即_.each()
回调中的参数名称。实际上这里只需要一个参数,就像第二个版本一样:
var arr = [];
_.each(a,function(o){
arr.push(_.pick(o,'name','id'));
});
But your second solution using _.map()
is nicer - very clean and seems perfectly fine.
但是您使用的第二个解决方案_.map()
更好 - 非常干净,看起来非常好。
The first version is actually how _.map()
works internally. Here's the source codefor _.map()
:
第一个版本实际上是如何_.map()
在内部工作的。这里的源代码为_.map()
:
// Return the results of applying the iterator to each element.
// Delegates to **ECMAScript 5**'s native `map` if available.
_.map = _.collect = function(obj, iterator, context) {
var results = [];
if (obj == null)
return results;
if (nativeMap && obj.map === nativeMap)
return obj.map(iterator, context);
each(obj, function(value, index, list) {
results.push(iterator.call(context, value, index, list));
});
return results;
};
As you can see, it's basically the same code as your first version along with some extra stuff.
如您所见,它与您的第一个版本的代码基本相同,但还有一些额外的东西。
You asked about rejecting keys; you can do that with the _.omit()
function, which is the opposite of _.pick()
: you list the property names you want to omit, not the ones you want to copy. So with your test data, this would produce the same thing as the other versions:
您询问了拒绝密钥的问题;你可以用_.omit()
函数来做到这一点,这与_.pick()
: 列出要省略的属性名称,而不是要复制的属性名称。因此,对于您的测试数据,这将产生与其他版本相同的结果:
_.map(a, function(o) { return _.omit(o, 'description'); });
The difference would be that if there were additional properties in the input data, this version would copy those properties into the output too. The version using _.pick()
would only have id
and name
in the output.
不同之处在于,如果输入数据中有其他属性,此版本也会将这些属性复制到输出中。使用的版本在输出中_.pick()
只会有id
和name
。
My only additional suggestion would that if you're dealing with very large arrays it would be a little faster to do the equivalent of _.pick()
yourself:
我唯一的额外建议是,如果你正在处理非常大的数组,那么做_.pick()
你自己的等价物会快一点:
var result = _.map( a, function( o ) {
return { name: o.name, id: o.id };
});
Or even faster to use your own loop:
或者甚至更快地使用您自己的循环:
var result = [];
for( var i = 0, n = a.length; i < n; ++i ) {
var o = a[i];
result[i] = { name: o.name, id: o.id };
}
But for a smallish array you may as well go with the simpler code.
但是对于较小的数组,您不妨使用更简单的代码。
回答by Alizadeh118
Also we can use Vanilla JSincluding map
, destructuring assignment
and arrow function
:
我们也可以使用Vanilla JS包括map
,destructuring assignment
和arrow function
:
const a = [
{
id: 1,
name: "First Name",
description: ""
},
{
id: 2,
name: "Second Name",
description: ""
}
]
// we need only id and name
const result = a.map(({id, name})=>({id, name}))
// result:
[
{
id: 1,
name: "First Name"
},
{
id: 2,
name: "Second Name"
}
]