Javascript 如何从javascript数组中选择特定字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7172277/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:10:30  来源:igfitidea点击:

How to select a particular field from javascript array

javascriptjqueryjquery-plugins

提问by Kavitha K Gowd

I have an array object in javascript. I would to select a particular field from all the rows of the object.

我在 javascript 中有一个数组对象。我想从对象的所有行中选择一个特定的字段。

I have an object like

我有一个像

var sample = {
[Name:"a",Age:1],
[Name:"b",Age:2],
[Name:"c",Age:3]
}

I would like to get an output of only Names as ["a","b","c"]without looping over sample object.

我想获得仅 Names 的输出,["a","b","c"]而不需要循环遍历示例对象。

How can I select one or two fields using jlinq? or any other plugin?

如何使用 jlinq 选择一两个字段?或任何其他插件?

Thanks a lot.

非常感谢。

采纳答案by Jamiec

You've got your definition the wrong way round. Instead of having an object containing 3 arrays, you want an array of objects.

你的定义是错误的。您需要一个对象数组,而不是包含 3 个数组的对象。

like this:

像这样:

var sample = [{Name:"a",Age:1},
   {Name:"b",Age:2},
   {Name:"c",Age:3}];

Then you can do:

然后你可以这样做:

var name0 = sample[0].Name;
var age0 = sample[0].Age;

or to get all your names as per your example:

或根据您的示例获取您的所有姓名:

var names = [sample[0].Name,sample[1].Name,sample[2].Name];

But, without looping im not sure how you would get any number of values.... why no looping?

但是,如果不循环,我不确定您将如何获得任意数量的值……为什么不循环?

Just say you do loop, here's how you would do it:

只是说你做循环,这是你的方法:

var names = []
for(x in sample)
   names.push(sample[x].Name);

or with jQuery (which is still looping)

或使用 jQuery(仍在循环中)

sample= jQuery.map(sample, function(n, i){
  return n.Name;
});

回答by Stéphan

You can try this:

你可以试试这个:

var sample = [{Name:"a", Age:1}, {Name:"b", Age:2}, {Name:"c", Age:3}];
var Names = sample.map(function(item){return item.Name;});

回答by Paul

That Javascript has no meaning. It is syntatically incorrect. I assume you meant:

那个 Javascript 没有意义。它在语法上是不正确的。我假设你的意思是:

var sample = [
    {Name:"a",Age:1},
    {Name:"b",Age:2},
    {Name:"c",Age:3}
]

Then you can use jQuery to do something like this:

然后你可以使用 jQuery 来做这样的事情:

var names = $(sample).map(function(){ return this.Name; });

But in reality all jQuery is doing is looping through the object for you. Writing your own loop would be (insignificantly) faster. There is no way to do this without a loop.

但实际上,jQuery 所做的只是为您遍历对象。编写自己的循环会(微不足道)更快。没有循环就没有办法做到这一点。

回答by cwallenpoole

// assuming you mean:
var sample = [
    {Name:"a",Age:1},
    {Name:"b",Age:2},
    {Name:"c",Age:3}
]

Well, your biggest problem there is that no matter what happens, you'll have to loop. Now, jQuery will let you hide that behavior with:

好吧,你最大的问题是无论发生什么,你都必须循环。现在,jQuery 将允许您通过以下方式隐藏该行为:

var output = []
$.each( sample, function(i,e){ output.push( e.Name )} );

But fundamentally, that is the same thing as:

但从根本上说,这与以下内容相同:

for( var i = 0; i < sample.length; i++ )
{
   output.push( v.Name )
}