Is there a javascript/jQuery counterpart to .NET's Enumerable.Select?
I have an array of javascript objects, each containing the members "Id" and "Name". Are there any built-in way in javascript/jQuery to project this array into another array, for example one that contains only the Names of the elements. In other words, someth开发者_开发问答ing similar to the Enumerable.Select method in .NET.
There's a LINQ for JavaScript project on Codeplex: http://linqjs.codeplex.com/
Another method is described here: http://www.ienablemuch.com/2011/05/jquerys-linqs-select.html
An excerpt from it is:
<script>
// This evil code was sourced from http://stackoverflow.com/questions/761148/jquery-document-ready-and-document-write/761190#761190
$(function () {
document.write = function (evil) {
$('body').append(evil);
}
});
// ...evil :p mwahahah
$(function () {
a = ["jumps", "over", "lazy", "dog"];
b = $.map(a, function (v) {
return "www." + v + ".com";
});
$.each(b, function () {
document.write(this + "<br/>");
});
i = 0;
c = $.map(b, function (v) {
return { v: v, i: ++i, m: i * 2 };
});
$.each(c, function () {
document.write(this.v + " xxx " + this.i + ' yyy ' + this.m + "<br/>");
});
});
</script>
Assuming your oldarray contains objects that have a Name attribute. The new array should contain just strings that are the names of the objects in the old array. Using Jquery for such a simple task might be an overkill
var oldarray;
var newarray;
oldarray.forEach( function(element)
{
newarray.push( element.Name);
});
精彩评论