Is there any function in jQuery that is equivalent to PHP's array_column()?

前端 未结 6 1333
轻奢々
轻奢々 2021-02-10 04:57

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.

6条回答
  •  迷失自我
    2021-02-10 05:54

    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];
        })
    }
    

提交回复
热议问题