重新排列数组以使键具有值

Re-arranging an array to have keys have values(重新排列数组以使键具有值)
本文介绍了重新排列数组以使键具有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Javascript对象数组,如下所示

const lab = [
  {
    name: "Lance",
    item: "PS",
    "3-Aug-21": "117.69",
    "4-Aug-21": "120.1",
    "5-Aug-21": "122.08",
  },
  {
    name: "Sam",
    item: "PS",
    "3-Aug-21": "100.14",
    "4-Aug-21": "101.2",
    "5-Aug-21": "103",
  },
  {
    name: "Vince",
    item: "PS",
    "3-Aug-21": "12.65",
    "4-Aug-21": "13.29",
    "5-Aug-21": "13.68",
  },
];

我将其修改如下

const labCheck = [
  {
    id: "Lance",
    values: [
      { date: "3-Aug-21", risklevel: "117.69" },
      { date: "4-Aug-21", risklevel: "120.1" },
      { date: "5-Aug-21", risklevel: "122.08" },
    ],
  },
  {
    id: "Sam",
    values: [
      { date: "3-Aug-21", risklevel: "100.14" },
      { date: "4-Aug-21", risklevel: "101.2" },
      { date: "5-Aug-21", risklevel: "103" },
    ],
  },
  {
    id: "Vince",
    values: [
      { date: "3-Aug-21", risklevel: "12.65" },
      { date: "4-Aug-21", risklevel: "13.29" },
      { date: "5-Aug-21", risklevel: "13.68" },
    ],
  },
];

我已经尝试了下面的代码

const labCheck = lab
  .map((d) => d.name)
  .map(function (name) {
    // console.log(id);
    return {
      id: name,
      values: Object.keys(lab[0])
        .slice(2)
        .map(function (d, id) {
          return {
            date: d,
            risklevel: +lab[id][d],
          };
        }),
    };
  });

它似乎起作用了,我得到了结构,但是riskLevel的值对于所有id都是相同的,这是不正确的。

我已经试着纠正这个问题好几天了,但是没有成功。 有谁能帮忙吗?

推荐答案

使用destructuring在重构的每个级别隔离并重命名我们感兴趣的属性。

const lab = [{ name: "Lance", item: "PS", "3-Aug-21": "117.69", "4-Aug-21": "120.1", "5-Aug-21": "122.08", }, { name: "Sam", item: "PS", "3-Aug-21": "100.14", "4-Aug-21": "101.2", "5-Aug-21": "103", }, { name: "Vince", item: "PS", "3-Aug-21": "12.65", "4-Aug-21": "13.29", "5-Aug-21": "13.68", },];

const labCheck = lab.map(({ name: id, item, ...values }) => ({
  id,
  values: Object.entries(values).map(([date, risklevel]) => ({ date, risklevel }))
}));

console.log(labCheck)
.as-console-wrapper { max-height: 100% !important; top: 0; }

如果需要在重构过程中变化值,则不能使用property shorthand,而需要显式声明每个属性并分配相关的变化值。

const lab = [{ name: "Lance", item: "PS", "3-Aug-21": "117.69", "4-Aug-21": "120.1", "5-Aug-21": "122.08", }, { name: "Sam", item: "PS", "3-Aug-21": "100.14", "4-Aug-21": "101.2", "5-Aug-21": "103", }, { name: "Vince", item: "PS", "3-Aug-21": "12.65", "4-Aug-21": "13.29", "5-Aug-21": "13.68", },];

const labCheck = lab.map(({ name: id, item, ...values }) => ({
  id,
  values: Object.entries(values).map(([date, risklevel]) => ({ 
    date: new Date(Date.parse(date)), // Date.parse() is not reliable, but used as example here.
    risklevel: +risklevel 
  }))
}));

console.log(labCheck)
.as-console-wrapper { max-height: 100% !important; top: 0; }

这篇关于重新排列数组以使键具有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Update another component when Formik form changes(当Formik表单更改时更新另一个组件)
Formik validation isSubmitting / isValidating not getting set to true(Formik验证正在提交/isValiating未设置为True)
React Validation Max Range Using Formik(使用Formik的Reaction验证最大范围)
Validation using Yup to check string or number length(使用YUP检查字符串或数字长度的验证)
Updating initialValues prop on Formik Form does not update input value(更新Formik表单上的初始值属性不会更新输入值)
password validation with yup and formik(使用YUP和Formick进行密码验证)