我想转换这样的对象:
{"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 中将对象转换为键值对数组?
我想转换这样的对象:
{"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.keys()
和map()
来做到这一点
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((key) => [Number(key), obj[key]]);
console.log(result);
最好的方法是这样做:
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.entries(obj);
console.log(result);
entries
如此处所示的Calling将[key, value]
按照调用者的要求返回对。
或者,您可以调用Object.values(obj)
,它只会返回值。
Object.entries()
返回一个数组,其元素是对应于[key, value]
直接在 上找到的可枚举属性对的数组object
。属性的顺序与通过手动循环对象的属性值给出的顺序相同。
Object.entries
除了键是字符串而不是数字外,该函数几乎返回您要求的确切输出。
const 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};
console.log(Object.entries(obj));
如果您需要键为数字,您可以将结果映射到一个带有回调函数的新数组,该函数将每对键中的键替换为从它强制转换的数字。
const 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};
const toNumericPairs = input => {
const entries = Object.entries(input);
return entries.map(entry => Object.assign(entry, { 0: +entry[0] }));
}
console.log(toNumericPairs(obj));
我Object.assign
在上面的例子中使用了一个箭头函数和map 回调,以便我可以通过利用Object.assign
返回被分配给的对象的事实将它保存在一个指令中,并且单个指令箭头函数的返回值是指令的结果。
这相当于:
entry => {
entry[0] = +entry[0];
return entry;
}
正如@TravisClarke 在评论中提到的,地图功能可以缩短为:
entry => [ +entry[0], entry[1] ]
但是,这将为每个键值对创建一个新数组,而不是修改现有数组,从而使创建的键值对数组数量加倍。虽然原始条目数组仍可访问,但它及其条目不会被垃圾收集。
现在,即使使用我们的就地方法仍然使用两个包含键值对的数组(输入和输出数组),但数组的总数只改变了一个。输入和输出数组实际上并不是用数组填充的,而是对数组的引用,而这些引用在内存中占用的空间可以忽略不计。
您可以更进一步,通过就地修改条目数组而不是将其映射到新数组来完全消除增长:
const 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};
const toNumericPairs = input => {
const entries = Object.entries(obj);
entries.forEach(entry => entry[0] = +entry[0]);
return entries;
}
console.log(toNumericPairs(obj));
现在在 2018 年回顾其中一些答案,其中 ES6 是标准。
从对象开始:
let const={"1":9,"2":8,"3":7,"4":6,"5":5,"6":4,"7":3,"8":2,"9":1,"10":0,"12":5};
const obj={"1":9,"2":8,"3":7,"4":6,"5":5,"6":4,"7":3,"8":2,"9":1,"10":0,"12":5};
console.log(Object.values(obj));
//[9,8,7,6,5,4,3,2,1,0,5]
const obj={"1":9,"2":8,"3":7,"4":6,"5":5,"6":4,"7":3,"8":2,"9":1,"10":0,"12":5};
console.log(Object.entries(obj));
//[["1",9],["2",8],["3",7],["4",6],["5",5],["6",4],["7",3],["8",2],["9",1],["10",0],["12",5]]
const obj={"1":9,"2":8,"3":7,"4":6,"5":5,"6":4,"7":3,"8":2,"9":1,"10":0,"12":5};
console.log(Object.entries(obj).map(([k,v])=>[+k,v]));
//[[1,9],[2,8],[3,7],[4,6],[5,5],[6,4],[7,3],[8,2],[9,1],[10,0],[12,5]]
const obj={"1":9,"2":8,"3":7,"4":6,"5":5,"6":4,"7":3,"8":2,"9":1,"10":0,"12":5};
console.log(Object.entries(obj).reduce((ini,[k,v])=>(ini[k]=v,ini),[]));
//[undefined,9,8,7,6,5,4,3,2,1,0,undefined,5]
最后一种方法,它还可以根据键的值重新组织数组顺序。有时这可能是所需的行为(有时不是)。但现在的优点是值被索引在正确的数组槽上,对它进行搜索是必不可少的和微不足道的。
最后(不是原始问题的一部分,但为了完整性),如果您需要使用键或值轻松搜索,但您不想要稀疏数组、没有重复和无需重新排序而无需转换为数字键(甚至可以访问非常复杂的键),那么数组(或对象)就不是您所需要的。我会推荐Map
:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
let r=new Map(Object.entries(obj));
r.get("4"); //6
r.has(8); //true
在 Ecmascript 6 中,
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 res = Object.entries(obj);
console.log(res);
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 res = Object.entries(obj);
console.log(res);