function Person(name, favouriteColour) {
this.Name = name;
this.FavouriteColour = favouriteColour;
}
var group = [];
group.push(new Person("Bob", "Green"));
group.push(new Person("Jane", "Red"));
group.push(new Person("Jack", "Blue"));
What could I do to get an array of Names from group?
我怎样才能从组中获取一系列名称?
group.??? -> ["Bob", "Jane", "Jack"]
In c#, the same as: group.ConvertAll<string>(m => m.Name)
在c#中,与:group.ConvertAll
4 个解决方案
#1
2
I'll offer the obvious one with straight javascript that works in all browsers:
我将提供明显的一个直接javascript,适用于所有浏览器:
var names = [];
for (var i = 0; i < group.length; i++) {
names.push(group[i].Name);
}
Or with jQuery (using it's .map
utility method):
或者使用jQuery(使用它的.map实用程序方法):
var names = $.map(group, function(item) {return(item.Name);});
Or, if you install a .map
shim to make sure the .map
Array method is available in all browsers:
或者,如果您安装.map填充程序以确保所有浏览器都可以使用.map Array方法:
var names = group.map(function(item) {return(item.Name);});
#2
3
I think you'll just have to loop over the array and get the names that way.
我认为你只需循环遍历数组并获得这样的名称。
function getKeysArray(key, objArray) {
var result = [], l = objArray.length;
for (var i = 0; i < l; i++) {
result.push(objArray[i][key]);
}
return result;
}
alert(getKeysArray("Name", group));
You could also try a seperate library like LINQ to JavaScript which looks quite useful.
您也可以尝试一个单独的库,如LINQ to JavaScript,看起来非常有用。
#3
2
You could use .map
, but it's not available in older browsers.
您可以使用.map,但它在旧版浏览器中不可用。
// names is group's Name properties
var names = group.map(function(value) { return value.Name; });
#4
1
In JavaScript 1.6 and later:
在JavaScript 1.6及更高版本中:
group.map(function(p) { return p.Name; });
group.map(function(p){return p.Name;});
#1
2
I'll offer the obvious one with straight javascript that works in all browsers:
我将提供明显的一个直接javascript,适用于所有浏览器:
var names = [];
for (var i = 0; i < group.length; i++) {
names.push(group[i].Name);
}
Or with jQuery (using it's .map
utility method):
或者使用jQuery(使用它的.map实用程序方法):
var names = $.map(group, function(item) {return(item.Name);});
Or, if you install a .map
shim to make sure the .map
Array method is available in all browsers:
或者,如果您安装.map填充程序以确保所有浏览器都可以使用.map Array方法:
var names = group.map(function(item) {return(item.Name);});
#2
3
I think you'll just have to loop over the array and get the names that way.
我认为你只需循环遍历数组并获得这样的名称。
function getKeysArray(key, objArray) {
var result = [], l = objArray.length;
for (var i = 0; i < l; i++) {
result.push(objArray[i][key]);
}
return result;
}
alert(getKeysArray("Name", group));
You could also try a seperate library like LINQ to JavaScript which looks quite useful.
您也可以尝试一个单独的库,如LINQ to JavaScript,看起来非常有用。
#3
2
You could use .map
, but it's not available in older browsers.
您可以使用.map,但它在旧版浏览器中不可用。
// names is group's Name properties
var names = group.map(function(value) { return value.Name; });
#4
1
In JavaScript 1.6 and later:
在JavaScript 1.6及更高版本中:
group.map(function(p) { return p.Name; });
group.map(function(p){return p.Name;});