是否有与 .NET 的 Enumerable.Select 相对应的 javascript/jQuery?

发布于 2024-11-09 18:52:09 字数 245 浏览 2 评论 0原文

我有一个 javascript 对象数组,每个对象都包含成员“Id”和“Name”。 javascript/jQuery 中是否有任何内置方法可以将此数组投影到另一个数组中,例如仅包含元素名称的数组。换句话说,类似于 .NET 中的 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, something similar to the Enumerable.Select method in .NET.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝咒 2024-11-16 18:52:09

摘录如下:

<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>

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>
左耳近心 2024-11-16 18:52:09

假设您的 oldarray 包含具有 Name 属性的对象。新数组应仅包含旧数组中对象名称的字符串。使用 Jquery 来完成如此简单的任务可能有点大材小用

    var oldarray;

    var newarray;

    oldarray.forEach( function(element) 
    {
        newarray.push( element.Name);
    });

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);
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文