【问题标题】:Map to Obejct Javascript convert in REACTJS映射到对象 Javascript 在 REACT JS 中转换
【发布时间】:2019-04-10 18:41:03
【问题描述】:

我想在 REACTJS 中将以下 Map 转换为 Object 我怎样才能做到这一点

[
      {
        "lastName": "Last name should have atleast 1 characters "
      },
      {
        "role": "may not be null"
      },
      {
        "name": "Name should have atleast 1 characters "
      }
    ]

转换成

{
  "lastName": "Last name should have atleast 1 characters ",
  "role": "may not be null",
  "name": "Name should have atleast 1 characters "
}

【问题讨论】:

  • 这是非常不规则的结构。理想情况下,您应该首先修复它。
  • 在第一个 Code sn-p 中,我使用了 Map,但我想要第二个 Snippet 之类的结构,你能帮我举个 codepen 中的例子吗

标签: arrays reactjs dictionary


【解决方案1】:

Array.prototype.reduce() 可用于从数组创建对象。

请参阅下面的实际示例。

// Input.
const input = [{lastName: 'Last name should have atleast 1 characters'}, {role: 'may not be null'}, {name: 'Name should have atleast 1 characters '}]

// Convert Array To Object.
const convertArrayToObject = array => array.reduce((acc, x) => {
  for (const key in x) acc[key] = x[key]
  return acc
}, {})

// Output + Proof.
const output = convertArrayToObject(input)
console.log(output)

【讨论】:

    【解决方案2】:

    您可以使用嵌套的 reduce,但如果数组中的键不是唯一的,则数组中较早的键将被后面的键覆盖:

    console.log(
      [
        {
          lastName:
            'Last name should have atleast 1 characters ',
        },
        {
          role: 'may not be null',
        },
        {
          name: 'Name should have atleast 1 characters ',
        },
      ].reduce((mainResult, item) =>
        Object.entries(item).reduce(
          (result, [key, value]) => (
            (result[key] = value), result
          ),
          mainResult,
        ),
        {}
      ),
    );

    看到你的答案(你应该评论而不是添加答案)看起来你的数组是一个 JSON 字符串数组,请尝试以下操作:

    console.log(
      //array of json strings
      [
        '{"lastName":"Last name should have atleast 1 characters "}',
        '{"role":"may not be null"}',
        '{"name":"Name should have atleast 1 characters "}',
      ]
        .map((item) => JSON.parse(item))
        .reduce((mainResult, item) =>
          Object.entries(item).reduce(
            (result, [key, value]) => (
              (result[key] = value), result
            ),
            mainResult,
          ),
          {}
        ),
    );

    【讨论】:

    • 为什么不删掉map(),只删掉reduce()中的JSON.parse()
    【解决方案3】:

    您需要减少数组以将其形状更改为所需的对象

    const a = [
      {
        "lastName": "Last name should have atleast 1 characters "
      },
      {
        "role": "may not be null"
      },
      {
        "name": "Name should have atleast 1 characters "
      }
    ];
    
    const b = a.reduce((acc, item) => {
      const key = Object.keys(item)[0];
      return {
        ...acc,
        [key]: item[key],
      };
    }, []);
    
    console.log(b);

    【讨论】:

      猜你喜欢
      • 2023-01-07
      • 2021-01-30
      • 1970-01-01
      • 2019-03-10
      • 1970-01-01
      • 2020-04-26
      • 2020-01-23
      • 1970-01-01
      • 2016-04-13
      相关资源
      最近更新 更多