如何创建具有多个键和元素的单个对象?

4

我有一个包含和其数组元素的数组。

当我将其从数组转换为对象时,我得到的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"
    }
  ]
}

如果我在convert函数内部定义数组并将其推入,那么我会再次得到index 0和1。如何在这里获得预期结果。
4个回答

2

展开语法...)是关键:

最初的回答

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.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" }] }],
    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
}

0

你只需要在对象中使用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))


一些IDE可能会拒绝您的新对象,相反请使用 let obj = {}; - mplungjan
我相信它与https://dev59.com/6rPma4cB1Zd3GeqPkArN#55530612完全相同。 - mplungjan

0

使用您的代码

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接