我想从数组中返回一个JSON对象

我有以下数组。

numbers = ["1","2"]

我想将此数组转换为以下对象

numberList = {"1" : [], "2" : []}

我试过如下。但它不起作用。我想将数字作为变量传递。

numbers.map( function(number) {
      return {number : []};
  })

回答

您可以映射条目并从中构建一个对象。

const
    numbers = ["1", "2"],
    object = Object.fromEntries(numbers.map(key => [key, []]));

console.log(object);


以上是我想从数组中返回一个JSON对象的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>