在下面的示例中,请帮助我知道如何提取用逗号分隔的每个数组元素:
array = [37.421998333333335: -122.08400000000002, 35.71529801212532: 51.39060813566725]
我尝试了切片,但在上述情况下不起作用。请帮助我知道解决方案。最后,我想将每个元素提取为一个对象,如下所示:
{'37.421998333333335': -122.08400000000002}
您的JavaScript无效。它必须是一个对象或一个数组,而不是两者的组合。
const
coords = { 37.421998333333335: -122.08400000000002, 35.71529801212532: 51.39060813566725 },
pairs = Object.entries(coords).map(entry => Object.fromEntries([entry]));
console.log(pairs);
.as-console-wrapper { top: 0; max-height: 100% !important; }
const
coords = [ 37.421998333333335, -122.08400000000002, 35.71529801212532, 51.39060813566725 ],
pairs = new Array(coords.length / 2).fill(0)
.reduce((r, e, i, a) => [...r, [ coords[i * 2], coords[i * 2 + 1] ]], [])
.map(entry => Object.fromEntries([entry]));
console.log(pairs);
.as-console-wrapper { top: 0; max-height: 100% !important; }
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句