【问题标题】:How to create single object with multiple keys and elements?如何创建具有多个键和元素的单个对象?
【发布时间】:2019-04-05 07:40:24
【问题描述】:

我有一个包含key 及其array 元素的数组

当我将它从数组转换为对象时,我得到 index 0 和 1 但这里我需要的索引应该是 array1array2

let input = [{
    "array1": [{
      "id": 1,
      "name": "Test 1"
    }, {
      "id": 2,
      "name": "Test 2"
    }]
  },
  {
    "array2": [{
        "id": 1,
        "name": "Test 1"
      },
      {
        "id": 2,
        "name": "Test 2"
      }
    ]
  }
];


function convert(arr) {
  let obj = new Object();

  arr.forEach((value, key) => {
    obj = value;
  });
  return obj;
}
console.log(convert(input))

所以在调用这个函数后我得到以下输出

{
  "array2": [
     {
      "id": 1,
      "name": "Test 1"
     },
    {
      "id": 2,
      "name": "Test 2"
    }
  ]
}

但是这里我需要的输出应该是这样的

{
  "array1": [
    {
      "id": 1,
      "name": "Test 1"
    },
    {
      "id": 2,
      "name": "Test 2"
    }
  ],
  "array2": [
    {
      "id": 1,
      "name": "Test 1"
    },
    {
      "id": 2,
      "name": "Test 2"
    }
  ]
}

如果我在转换函数中使用定义数组并推送它,那么我再次得到index 0 和 1。

我怎样才能在这里得到预期的结果。

【问题讨论】:

    标签: javascript arrays json object


    【解决方案1】:

    Spreading (...) 是关键:

    let input = [{
        "array1": [{
          "id": 1,
          "name": "Test 1"
        }, {
          "id": 2,
          "name": "Test 2"
        }]
      },
      {
        "array2": [{
            "id": 1,
            "name": "Test 1"
          },
          {
            "id": 2,
            "name": "Test 2"
          }
        ]
      }
    ];
    
    
    function convert(arr) {
      return arr.reduce((acc, curr) => ({ ...acc, ...curr }), {});
    }
    console.log(convert(input))
    .as-console-wrapper { max-height: 100% !important; top: auto; }

    您也可以将它与数组本身的Object.assign 结合起来:

    let input = [{
        "array1": [{
          "id": 1,
          "name": "Test 1"
        }, {
          "id": 2,
          "name": "Test 2"
        }]
      },
      {
        "array2": [{
            "id": 1,
            "name": "Test 1"
          },
          {
            "id": 2,
            "name": "Test 2"
          }
        ]
      }
    ];
    
    
    function convert(arr) {
      return Object.assign(...arr);
    }
    console.log(convert(input))
    .as-console-wrapper { max-height: 100% !important; top: auto; }

    【讨论】:

      【解决方案2】:

      要获得具有所需键的单个对象,您可以使用 Object.assignspread syntax ... 将所有项目分配给单个对象。

      let input = [{ array1: [{ id: 1, name: "Test 1" }, { id: 2, name: "Test 2" }] }, { array2: [{ id: 1, name: "Test 1" }, { id: 2, name: "Test 2" }] }],
          object = Object.assign(...input);
      
      console.log(object)
      .as-console-wrapper { max-height: 100% !important; top: 0; }

      您的代码不起作用,因为您将最新的作业作为结果

      function convert(arr) {
          let obj = new Object();       // initialization
      
          arr.forEach((value, key) => {
              obj = value;              // assignment
          });
          return obj;                   // returns last assignment
      }
      

      【讨论】:

        【解决方案3】:

        使用您的代码

        function convert(arr) {
           let obj = new Object();
           arr.forEach((value, key) => {
             obj[key] = value;
           });
           return obj;
        }
        

        您在 forEach 的每次迭代中都在执行 obj = value;,这意味着您每次都将 obj 重新分配为新值。

        【讨论】:

          【解决方案4】:

          您只需要在对象中使用 foreach 函数的关键参数即可。我附上下面的代码你可以看看。

          let input = [{
              "array1": [{
                "id": 1,
                "name": "Test 1"
              }, {
                "id": 2,
                "name": "Test 2"
              }]
            },
            {
              "array2": [{
                  "id": 1,
                  "name": "Test 1"
                },
                {
                  "id": 2,
                  "name": "Test 2"
                }
              ]
            }
          ];
          
          
          function convert(arr) {
            let obj = new Object();
          
            arr.forEach((value, key) => {
              obj[key] = value;
            });
            return obj;
          }
          console.log(convert(input))

          【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多