如何将 JavaScript 中的 Object {} 转换为 key-value 对的 Array []?

462

我想将一个像这样的对象转换:

{"1":5,"2":7,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0}

成为这样的键值对数组:

[[1,5],[2,7],[3,0],[4,0]...].

如何将JavaScript中的Object转换为键-值对数组?

23个回答

7
如果您正在使用lodash,那么它可能就是这么简单:
var arr = _.values(obj);

5
var obj = { "1": 5, "2": 7, "3": 0, "4": 0, "5": 0, "6": 0, "7": 0, "8": 0, "9": 0, "10": 0, "11": 0, "12": 0 }
let objectKeys = Object.keys(obj);

let answer = objectKeys.map(value => {
    return [value + ':' + obj[value]]
});

4
const persons = { 
    john: { age: 23, year:2010},
    jack: { age: 22, year:2011},
    jenny: { age: 21, year:2012}
}
const resultArray = Object.keys(persons).map(index => {
    let person = persons[index];
    return person;
});

//use this for not indexed object to change array

3

或者你可以使用Object.assign():

const obj = { 0: 1, 1: 2, 2: 3};
const arr = Object.assign([], obj);
console.log(arr)
// arr is [1, 2, 3]


3

这是我的解决方案,我也遇到了同样的问题,看起来这个解决方案对我很有效。

yourObj = [].concat(yourObj);

2

您可以使用三种方法将对象转换为数组,它们分别是:Object.keys()Object.values()Object.entries()

三种方法的示例:

使用 Object.keys()

const text= {
    quote: 'hello world',
    author: 'unknown'
};

const propertyNames = Object.keys(text);

console.log(propertyNames);

result
[ 'quote', 'author' ]

使用Object.values()

const propertyValues = Object.values(text);

console.log(propertyValues);

结果

[ 'Hello world', 'unknown' ]

使用 Object.entries()

const propertyValues = Object.entires(text);

console.log(propertyValues);

结果

[ [ 'quote', 'Hello world' ], [ 'author', 'unknown' ] ]

虽然这很有信息量,但问题更具体,只需通过第三部分就可以回答。请考虑编辑您的答案以回答问题。 - Sebastian
是的,在回答问题之前我确实提到了上面,无论如何我都加粗了它(我提供了3个答案作为参考,对于那些需要学习这种方法的人可能您是专业人士,但我提到它是为了初学者)。 - Dulanjana Abeyrathna

2
这是一种使用es6的“新”方法,它结合了spread运算符和Object.entries
const data = {"1":5,"2":7,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0};

const dataSpread = Object.entries(data);

// data spread value is now:
[
  [ '1', 5 ],  [ '2', 7 ],
  [ '3', 0 ],  [ '4', 0 ],
  [ '5', 0 ],  [ '6', 0 ],
  [ '7', 0 ],  [ '8', 0 ],
  [ '9', 0 ],  [ '10', 0 ],
  [ '11', 0 ], [ '12', 0 ]
]

这里使用展开运算符是多余的。因为Object.entries()已经返回了符合预期格式的2D数组,所以不需要使用它。 - Niket Pathak
没错,你是对的。外部数组也不需要,所以我已经更新了代码,把它也去掉了! - brff19

0
我们可以像下面这样将数字更改为字符串类型的键:

var obj = {"1":5,"2":7,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0}
var result = Object.keys(obj).map(function(key) {
  return [String(key), obj[key]];
});
    
console.log(result);


0
使用for in语句。
var obj = { "10":5, "2":7, "3":0, "4":0, "5":0, "6":0, "7":0,
            "8":0, "9":0, "10":0, "11":0, "12":0 };

var objectToArray = function(obj) {
    var _arr = [];

    for (var key in obj) {
        _arr.push([key, obj[key]]);
    }
    return _arr;
}

console.log(objectToArray(obj));

0

递归将对象转换为数组

function is_object(mixed_var) {
    if (mixed_var instanceof Array) {
        return false;
    } else {
        return (mixed_var !== null) && (typeof( mixed_var ) == 'object');
    }
}


function objectToArray(obj) {
    var array = [], tempObject;
    for (var key in obj) {

        tempObject = obj[key];

        if (is_object(obj[key])) {
            tempObject = objectToArray(obj[key]);
        }
        array[key] = tempObject;
    }
    return array;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接