我有这个数组:
[ [ '560134275538747403', 39953 ],
[ '411510958020624384', 36164 ],
[ '468512396948930576', 31762 ],
[ '482286641982078977', 29434 ],
[ '249892869127536641', 6295 ] ]
我想将其隐藏成这样的对象:
{
"560134275538747403":39953,
"411510958020624384":36164,
"468512396948930576":31762,
"482286641982078977":29434,
"249892869127536641":6295
}
无论如何要这样做?
〜和thx
最佳答案
使用Object.fromEntries
:
const arr=[['560134275538747403',39953],['411510958020624384',36164],['468512396948930576',31762],['482286641982078977',29434],['249892869127536641',6295]];
const res = Object.fromEntries(arr);
console.log(res);
或者,由于不受广泛支持,所以
reduce
:const arr=[['560134275538747403',39953],['411510958020624384',36164],['468512396948930576',31762],['482286641982078977',29434],['249892869127536641',6295]];
const res = arr.reduce((a, [k, v]) => (a[k] = v, a), {});
console.log(res);