根据键的值转换/旋转对象数组,并将其重新转换回 JavaScript 中的原始值

问题描述 投票:0回答:1

我希望能够使用统一的键转换下面的数组或对象,但将其旋转或转换为对象数组,但对象数量与对象中的属性一样多,每个对象现在都具有属性名称以及原始数组中指定键的每个值的键(在本例中是原始数组中“年份”的所有可能值)。我也想把这个数组重新转换成原始形式。

我想知道这是否可以以通用方式完成,以便可以在对象的任何时间并基于其任何键的值来完成。

输入:

[
    {
        "property0": "property0_2024",
        "property1": "property1_2024",
        "property2": "property2_2024",
        "property3": "property3_2024",
        "property4": "property4_2024",
        "year": "2024"
    },
    {
        "property0": "property0_2025",
        "property1": "property1_2025",
        "property2": "property2_2025",
        "property3": "property3_2025",
        "property4": "property4_2025",
        "year": "2025"
    },
    {
        "property0": "property0_2026",
        "property1": "property1_2026",
        "property2": "property2_2026",
        "property3": "property3_2026",
        "property4": "property4_2026",
        "year": "2026"
    },
    {
        "property0": "property0_2027",
        "property1": "property1_2027",
        "property2": "property2_2027",
        "property3": "property3_2027",
        "property4": "property4_2027",
        "year": "2027"
    }
]

输出:

[
    {
        "propertyName": "property0",
        "year_2024": "property0_2024",
        "year_2025": "property0_2025",
        "year_2026": "property0_2026",
        "year_2027": "property0_2027"
    },
    {
        "propertyName": "property1",
        "year_2024": "property1_2024",
        "year_2025": "property1_2025",
        "year_2026": "property1_2026",
        "year_2027": "property1_2027"
    },
    {
        "propertyName": "property2",
        "year_2024": "property2_2024",
        "year_2025": "property2_2025",
        "year_2026": "property2_2026",
        "year_2027": "property2_2027"
    },
    {
        "propertyName": "property3",
        "year_2024": "property3_2024",
        "year_2025": "property3_2025",
        "year_2026": "property3_2026",
        "year_2027": "property3_2027"
    },
    {
        "propertyName": "property4",
        "year_2024": "property4_2024",
        "year_2025": "property4_2025",
        "year_2026": "property4_2026",
        "year_2027": "property4_2027"
    }
]
javascript arrays multidimensional-array pivot-table transpose
1个回答
0
投票

是的,您可以围绕指定的键旋转数据。

使用

Set
唯一存储您的属性,过滤掉重复项。 然后,只需生成一个新数组,其中每个条目都是一个新对象,其值取自输入数组。

这是一个例子:

function pivotData(inputArray, pivotKey, propertyKeyName = 'propertyName') {
  // Consider only unique properties and exclude the pivotKey
  const properties = new Set();
  const pivotValues = [];

  inputArray.forEach(item => {
      Object.keys(item).forEach(key => {
          if (key !== pivotKey) {
              properties.add(key);
          }
      });
      pivotValues.push(item[pivotKey]);
  });

  const result = [];
  properties.forEach(property => {
      const obj = {};
      obj[propertyKeyName] = property;
      inputArray.forEach(item => {
          const pivotValue = item[pivotKey];
          const key = `${pivotKey}_${pivotValue}`;
          obj[key] = item[property];
      });
      result.push(obj);
  });

  return result;
}
© www.soinside.com 2019 - 2024. All rights reserved.