JavaScript对象将对象键移动到值阵列

发布于 2025-01-18 18:52:05 字数 823 浏览 0 评论 0 原文

如何将键传输到作为对象的值。 我想将产品 ID 级别的键移动到“id”值

我有一个像这样的数组:

const products = [
  {
    "2": {
      "name": "Name of product with id 2",
      "ean": "123123123",
      "parameters": []
      },
    "3": {
      "name": "Name of product with id 3",
      "ean": "123123122",
      "parameters": []
      },
    "5": {
      "name": "Name of product with id 5",
      "ean": "123123121",
      "parameters": []
      }
  }
]

我期待这样:

[
  {
    "id": "2",
    "name": "Name of product with id 2",
    "ean": "123123123",
    "parameters": []
  },
  {
    "id": "3",
    "name": "Name of product with id 3",
    "ean": "123123122",
    "parameters": []
  },
  {
    "id": "5",
    "name": "Name of product with id 5",
    "ean": "123123121",
    "parameters": []
  }
]

how can I transfer a key to a value which is an object.
I would like to move key which is the product ID level down to a value as "id"

I have an array like this:

const products = [
  {
    "2": {
      "name": "Name of product with id 2",
      "ean": "123123123",
      "parameters": []
      },
    "3": {
      "name": "Name of product with id 3",
      "ean": "123123122",
      "parameters": []
      },
    "5": {
      "name": "Name of product with id 5",
      "ean": "123123121",
      "parameters": []
      }
  }
]

I am expecting this:

[
  {
    "id": "2",
    "name": "Name of product with id 2",
    "ean": "123123123",
    "parameters": []
  },
  {
    "id": "3",
    "name": "Name of product with id 3",
    "ean": "123123122",
    "parameters": []
  },
  {
    "id": "5",
    "name": "Name of product with id 5",
    "ean": "123123121",
    "parameters": []
  }
]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦屿孤独相伴 2025-01-25 18:52:05
const products = [
  {
    "2": { "name": "Name of product with id 2", "ean": "123123123", "parameters": [] },
    "3": { "name": "Name of product with id 3", "ean": "123123122", "parameters": [] },
    "5": { "name": "Name of product with id 5", "ean": "123123121", "parameters": [] }
  }
];

const res = products
  .flatMap(Object.entries)
  .map(([ id, props ]) => ({ ...props, id }));

console.log(res);

const products = [
  {
    "2": { "name": "Name of product with id 2", "ean": "123123123", "parameters": [] },
    "3": { "name": "Name of product with id 3", "ean": "123123122", "parameters": [] },
    "5": { "name": "Name of product with id 5", "ean": "123123121", "parameters": [] }
  }
];

const res = products
  .flatMap(Object.entries)
  .map(([ id, props ]) => ({ ...props, id }));

console.log(res);

追风人 2025-01-25 18:52:05

您只需使用 array。 map()方法。

演示:

const products = [
  {
    "2": {
      "name": "Name of product with id 2",
      "ean": "123123123",
      "parameters": []
      },
    "3": {
      "name": "Name of product with id 3",
      "ean": "123123122",
      "parameters": []
      },
    "5": {
      "name": "Name of product with id 5",
      "ean": "123123121",
      "parameters": []
      }
  }
];

const res = Object.keys(products[0]).map((id) => {
    products[0][id]['id'] = id;
  return products[0][id];
});

console.log(res);

You can simply achieve that by using Object.keys() along with Array.map() method.

Demo :

const products = [
  {
    "2": {
      "name": "Name of product with id 2",
      "ean": "123123123",
      "parameters": []
      },
    "3": {
      "name": "Name of product with id 3",
      "ean": "123123122",
      "parameters": []
      },
    "5": {
      "name": "Name of product with id 5",
      "ean": "123123121",
      "parameters": []
      }
  }
];

const res = Object.keys(products[0]).map((id) => {
    products[0][id]['id'] = id;
  return products[0][id];
});

console.log(res);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文