Javascript 散列键/值作为数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10415133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:12:56  来源:igfitidea点击:

hash keys / values as array

javascripthashmap

提问by greg0ire

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

我找不到相当于 php array_keys()/的 javascriptarray_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。

采纳答案by Imp

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

回答by

In ES5 supported (or shimmed)browsers...

在 ES5 支持(或填充)的浏览器中......

var keys = Object.keys(myHash);

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


Shims from MDN...

来自 MDN 的垫片...

回答by Literal

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.

无论如何,我总是尽可能地发挥作用。单线比循环更好。

回答by Josh Petitt

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

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

回答by Abid

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

回答by Igor Deruga

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

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

回答by Surreal Dreams

Here are implementations from phpjs.org:

以下是来自phpjs.org 的实现:

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

不是我的代码,我只是给你指出一个有用的资源。

回答by test30

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)

回答by VisioN

Here is a good example of array_keysfrom PHP.js library:

这是array_keys来自PHP.js 库的一个很好的例子:

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.

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