javascript - Javascript - 如何将数组中对象的第一个元素分配为另一个数组的键
问题描述
基本上我有一个数组:
我填写了一个 SQL 查询。这使得数组如下:
我想创建另一个数组,其中数组对象的键是组织内对象的第一个元素,因此它们将是 id1、id2 等。
我认为输出应该是这样的:
newArray = [id1: {name1, type2}, id2: {name2, type2}]
解决方案
试试那个
const a = [
{ id: 1, name: "peter", city: "New City" },
{ id: 2, name: "alex", city: "New City" },
{ id: 3, name: "michael", city: "New City" }
];
const b = a.map(item => {
const { id, ...others } = item;
return { id: { ...others } };
});
console.log(b);
推荐阅读
- python-3.x - 如何让我的虚拟助理听到我的声音?
- php - 显示 view_product 表中产品的查看次数
- javascript - 如何使用javascript在firebase中添加用户的额外信息,例如显示名称?
- laravel - 在laravel项目中滚动时如何使网页加载
- reactjs - 替换状态数组后使用useEffect更新子组件的正确方法
- excel-formula - 获取单元格的格式化值以用于左右功能
- discord.js - 如何让 discord.js 机器人在不包含前缀和命令名称的情况下重复用户的消息?
- flutter - 如何在android studio中获取flutter sdk路径?
- python - 使用 Python 脚本将 CSV 文件上传到 DynamoDB
- python - python中基于单词和非单词的字符串替换