jQuery中是否有任何函数等同于PHP的array_column()? [英] Is there any function in jQuery that is equivalent to PHP's array_column()?

查看:1154
本文介绍了jQuery中是否有任何函数等同于PHP的array_column()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jQuery中PHP的 array_column()相当于什么?我需要数组中的数据而不需要循环,就像在PHP中一样。

What is the equivalent to PHP's array_column() in jQuery? I need the data inside the array without looping, in the same way as in PHP.

推荐答案

您可以使用 .map()。 Immagine获取数据库行。

You can do it with .map(). Immagine a fetch of database rows.

var array = [
    {
        id: 1,
        name: 'foo'
    },
    {
        id: 2,
        name: 'bar'
    },
];
var names = arrayColumn(array, 'name');
console.log(names);

function arrayColumn(array, columnName) {
    return array.map(function(value,index) {
        return value[columnName];
    })
}

这篇关于jQuery中是否有任何函数等同于PHP的array_column()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆