【问题标题】:Javascript - Create object where key is surrounded by doublequotesJavascript - 创建键被双引号包围的对象
【发布时间】:2019-01-13 08:41:44
【问题描述】:

我有初始数组,应该迭代并创建新对象。初始数组为:

let array = [
    {key: "key1", translation: "some text 1"},
    {key2: "key2", translation: "some text 2"},
    {key3: "key3", translation: "some text 3"},
];

然后我迭代它并创建对象:

const final = {};
const language = "eng";

for (let item of array) {
    final[item.key] = {};
    final[item.key][language] = item.translation;
}

它最终成为对象:

{
  key1: {
    eng: "Some text 1"
  },
  key2: {
    eng: "Some text 2"
  },
  key3: {
    eng: "Some text 3"
  }
}

我需要将这个外部键(key1key2key3)用双引号括起来,所以最终对象变为:

{
  "key1": {
    eng: "Some text 1"
  },
  "key2": {
    eng: "Some text 2"
  },
  "key3": {
    eng: "Some text 3"
  }
}

【问题讨论】:

  • 不明白这个问题?! JSON.stringify(array) 会做你想做的事吗?!
  • “我最终得到”...你呢?也许这就是你用来检查它的显示方式,因为那时它是一个对象,而不是 JSON。 JSON 是文本。 JSON.stringify() 将产生一个有效的 JSON 字符串。目前尚不清楚您是如何得出结论的,或者您真正担心的是什么。
  • 为什么需要这么奇怪的格式?
  • “外部键被双引号,内部键(语言)应该保持不被引用”......这不是有效的 JSON,所以不清楚为什么你会认为这是可取的。你有什么可能的理由想要那个?我看不出它对以后有什么帮助,它所做的一切使得你发送 JSON 的任何程序都不太可能正确读取它。
  • 是的,您需要了解对象和 JSON 之间的区别。 JSON 是对象的文本表示。调试代码时,您还可以检查对象并查看对象的文本表示。它可能碰巧也可能不是 JSON 表示。但一般来说,当您想将数据发送到应用程序之外的某个地方时,您只需要主动创建 JSON,例如在 AJAX 请求中。如果您只是将其传递给更多 JS 代码,例如正如您提到的将其提供给 React 方法,然后将其保留为对象。

标签: javascript json object key double-quotes


【解决方案1】:

我认为您正在寻找JSON.stringify()。让我们看看这里:

let array = [
{key: "key1", translation: "some text 1"},
{key2: "key2", translation: "some text 2"},
{key3: "key3", translation: "some text 3"},
];
console.log(JSON.stringify(array));

【讨论】:

  • 正如我在上面已经写过的,JSON.stringify 生成字符串,其中所有键(外部和内部都被引用),这不是我要求的。但是,我不需要字符串作为最终输出,而是对象:)
【解决方案2】:

如果您使用 JSON.stringify() 获取您的数组的 JSON 字符串,则这些值将被完美引用:

let array = {
  key1: {
    eng: "Some text 1"
  },
  key2: {
    eng: "Some text 2"
  },
  key3: {
    eng: "Some text 3"
  }
};

console.log(JSON.stringify(array));

【讨论】:

    【解决方案3】:

    您编写的循环不正确。应该是这样的:

    let array = [
    {key: "key1", translation: "some text 1"},
    {key2: "key2", translation: "some text 2"},
    {key3: "key3", translation: "some text 3"},
    ];
    
    const final = {};
    const language = "eng";
    
    for (let item of array) {
        final[Object.values(item)[0]] = {};
        final[Object.values(item)[0]][language] = item.translation;
    }
    
    console.log(JSON.stringify(final));

    另外,不要忘记使用JSON.stringify() 进行字符串化。

    【讨论】:

      【解决方案4】:
      let array = [
          {key: "key1", translation: "some text 1"},
          {key: "key2", translation: "some text 2"},
          {key: "key3", translation: "some text 3"},
      ];
      
          let data={};
          array.map(item => {
              let d=item.key;
              let da=item.translation;
              data[d]=da;
          })
      }
      
      console.log(data)
      

      【讨论】:

      • 1.这与问题中的工作示例有何不同? 2. .map() 不应该这样使用。
      猜你喜欢
      • 1970-01-01
      • 2017-01-30
      • 2021-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多