散列键/值作为数组[重复]

时间:2021-11-26 07:37:47

This question already has an answer here:

这个问题在这里已有答案:

I cannot find the javascript equivalent of php array_keys() / array_values()

我找不到javascript相当于php array_keys()/ array_values()

For people unfamiliar with php given the following js hash:

对于不熟悉php的人给出以下js哈希:

var myHash = {"apples": 3, "oranges": 4, "bananas": 42}

How can I get an array of keys, i.e.

我怎样才能获得一系列密钥,即

["apples", "oranges", "bananas"]

Same question with the values, i.e.

与价值观相同的问题,即

[3, 4, 42]

jQuery can be used.

可以使用jQuery。

9 个解决方案

#1


44  

var a = {"apples": 3, "oranges": 4, "bananas": 42};    

var array_keys = new Array();
var array_values = new Array();

for (var key in a) {
    array_keys.push(key);
    array_values.push(a[key]);
}

alert(array_keys);
alert(array_values);

#2


66  

In ES5 supported (or shimmed) browsers...

在ES5支持(或shimmed)浏览器...

var keys = Object.keys(myHash);

var values = keys.map(function(v) { return myHash[v]; });

Shims from MDN...

来自MDN的垫片......

#3


14  

The second answer (at the time of writing) gives :

第二个答案(在撰写本文时)给出:

var values = keys.map(function(v) { return myHash[v]; });

But I prefer using jQuery's own $.map :

但我更喜欢使用jQuery自己的$ .map:

var values = $.map(myHash, function(v) { return v; });

Since jQuery takes care of cross-browser compatibility. Plus it's shorter :)

由于jQuery负责跨浏览器兼容性。加上它更短:)

At any rate, I always try to be as functional as possible. One-liners are nicers than loops.

无论如何,我总是尽量保持功能。单行比循环更好。

#4


9  

look at the _.keys() and _.values() functions in either lodash or underscore

查看lodash或下划线中的_.keys()和_.values()函数

#5


3  

function getKeys(obj){
    var keys = [];
    for (key in obj) {
        if (obj.hasOwnProperty(key)) { keys[keys.length] = key; }
    } 
    return keys;
}

#6


2  

Don't know if it helps, but the "foreach" goes through all the keys: for (var key in obj1) {...}

不知道它是否有帮助,但“foreach”遍历所有键:for(obj1中的var键){...}

#7


1  

Here are implementations from phpjs.org:

以下是phpjs.org的实现:

This is not my code, I'm just pointing you to a useful resource.

这不是我的代码,我只是指向一个有用的资源。

#8


1  

var myHash = {"apples": 3, "oranges": 4, "bananas": 42}
vals=(function(e){a=[];for (var i in e) a.push(e[i]); return a;})(myHash).join(',')
keys=(function(e){a=[];for (var i in e) a.push(  i ); return a;})(myHash).join(',')
console.log(vals,keys)

basically

基本上

array=(function(e){a=[];for (var i in e) a.push(e[i]); return a;})(HASHHERE)

#9


0  

Here is a good example of array_keys from PHP.js library:

以下是PHP.js库中array_keys的一个很好的例子:

function array_keys (input, search_value, argStrict) {
    // Return just the keys from the input array, optionally only for the specified search_value

    var search = typeof search_value !== 'undefined',
        tmp_arr = [],
        strict = !!argStrict,
        include = true,
        key = '';

    for (key in input) {
        if (input.hasOwnProperty(key)) {
            include = true;
            if (search) {
                if (strict && input[key] !== search_value) {
                    include = false;
                }
                else if (input[key] != search_value) {
                    include = false;
                }
            }

            if (include) {
                tmp_arr[tmp_arr.length] = key;
            }
        }
    }

    return tmp_arr;
}

The same goes for array_values (from the same PHP.js library):

array_values(来自相同的PHP.js库)也是如此:

function array_values (input) {
    // Return just the values from the input array  

    var tmp_arr = [],
        key = '';

    for (key in input) {
        tmp_arr[tmp_arr.length] = input[key];
    }

    return tmp_arr;
}

EDIT: Removed unnecessary clauses from the code.

编辑:从代码中删除不必要的子句。

#1


44  

var a = {"apples": 3, "oranges": 4, "bananas": 42};    

var array_keys = new Array();
var array_values = new Array();

for (var key in a) {
    array_keys.push(key);
    array_values.push(a[key]);
}

alert(array_keys);
alert(array_values);

#2


66  

In ES5 supported (or shimmed) browsers...

在ES5支持(或shimmed)浏览器...

var keys = Object.keys(myHash);

var values = keys.map(function(v) { return myHash[v]; });

Shims from MDN...

来自MDN的垫片......

#3


14  

The second answer (at the time of writing) gives :

第二个答案(在撰写本文时)给出:

var values = keys.map(function(v) { return myHash[v]; });

But I prefer using jQuery's own $.map :

但我更喜欢使用jQuery自己的$ .map:

var values = $.map(myHash, function(v) { return v; });

Since jQuery takes care of cross-browser compatibility. Plus it's shorter :)

由于jQuery负责跨浏览器兼容性。加上它更短:)

At any rate, I always try to be as functional as possible. One-liners are nicers than loops.

无论如何,我总是尽量保持功能。单行比循环更好。

#4


9  

look at the _.keys() and _.values() functions in either lodash or underscore

查看lodash或下划线中的_.keys()和_.values()函数

#5


3  

function getKeys(obj){
    var keys = [];
    for (key in obj) {
        if (obj.hasOwnProperty(key)) { keys[keys.length] = key; }
    } 
    return keys;
}

#6


2  

Don't know if it helps, but the "foreach" goes through all the keys: for (var key in obj1) {...}

不知道它是否有帮助,但“foreach”遍历所有键:for(obj1中的var键){...}

#7


1  

Here are implementations from phpjs.org:

以下是phpjs.org的实现:

This is not my code, I'm just pointing you to a useful resource.

这不是我的代码,我只是指向一个有用的资源。

#8


1  

var myHash = {"apples": 3, "oranges": 4, "bananas": 42}
vals=(function(e){a=[];for (var i in e) a.push(e[i]); return a;})(myHash).join(',')
keys=(function(e){a=[];for (var i in e) a.push(  i ); return a;})(myHash).join(',')
console.log(vals,keys)

basically

基本上

array=(function(e){a=[];for (var i in e) a.push(e[i]); return a;})(HASHHERE)

#9


0  

Here is a good example of array_keys from PHP.js library:

以下是PHP.js库中array_keys的一个很好的例子:

function array_keys (input, search_value, argStrict) {
    // Return just the keys from the input array, optionally only for the specified search_value

    var search = typeof search_value !== 'undefined',
        tmp_arr = [],
        strict = !!argStrict,
        include = true,
        key = '';

    for (key in input) {
        if (input.hasOwnProperty(key)) {
            include = true;
            if (search) {
                if (strict && input[key] !== search_value) {
                    include = false;
                }
                else if (input[key] != search_value) {
                    include = false;
                }
            }

            if (include) {
                tmp_arr[tmp_arr.length] = key;
            }
        }
    }

    return tmp_arr;
}

The same goes for array_values (from the same PHP.js library):

array_values(来自相同的PHP.js库)也是如此:

function array_values (input) {
    // Return just the values from the input array  

    var tmp_arr = [],
        key = '';

    for (key in input) {
        tmp_arr[tmp_arr.length] = input[key];
    }

    return tmp_arr;
}

EDIT: Removed unnecessary clauses from the code.

编辑:从代码中删除不必要的子句。