将一个包含数组的对象转换为React中的单独对象。

huangapple go评论67阅读模式
英文:

convert an object of arrays into individual objects in react

问题

以下是您要翻译的内容:

"SUMMARY_TABLE"中包含对象数组,我想从这些数组值中创建单独的对象,

"SUMMARY_TABLE": {
    "PRODUCT_CODE": [
        123,
        123,
        123,
        123,
        123
    ],
    "TYPE": [
        "CURRENT",
        "OPTIMAL",
        "MINIMUM",
        "MAXIMUM",
        "FUTURE"
    ],
    "LOT_SIZE": [
        268.0,
        268.0,
        268.0,
        268.0,
        500.0
    ]
}

以上是我得到的响应。我想从这些数据中创建5个单独的对象,如下所示:

{
    "product_code": 123,
    "type": "current",
    "lot_size": 268
}

类似地,使用数组中的其他索引处的值。我正在尝试这样做,但它没有给我我需要的结果。k 包含键名和值数组,例如 ['Product_Code', Arr(5)]

const data = Object.entries(selectedProductDetails.SUMMARY_TABLE).map(
    (k, v) => {
        let obj = {};
        return {
            ...obj, k: k[i++]
        }
    });

我会感激任何帮助。

英文:

I have an object of arrays, I want to create individual objects from those array values,

"SUMMARY_TABLE": {
                "PRODUCT_CODE": [
                    123,
                    123,
                    123,
                    123,
                    123
                ],
                "TYPE": [
                    "CURRENT",
                    "OPTIMAL",
                    "MINIMUM",
                    "MAXIMUM",
                    "FUTURE"
                ],
                "LOT_SIZE": [
                    268.0,
                    268.0,
                    268.0,
                    268.0,
                    500.0
                ]}

above is the response Im getting. I want to create 5 individual objects from this data like

{
"product_code": 123,
"type": "current",
"lot_size" : 268
}

similarly using values in the arrays at other indexes.
I am trying this but it is not giving me the result I need. k contains the key name and array of values, like ['Product_Code', Arr(5)]

const data = Object.entries(selectedProductDetails.SUMMARY_TABLE).map(
    (k, v) => {
      let obj = {};
      return {
        ...obj, k: k[i++]
      }
    });

I would appreciate any help in this.

答案1

得分: 1

你可以使用.map为每个元素创建一个新的object

以下是一个示例:

const summaryTable = {
  "PRODUCT_CODE": [
      123,
      123,
      123,
      123,
      123
  ],
  "TYPE": [
      "CURRENT",
      "OPTIMAL",
      "MINIMUM",
      "MAXIMUM",
      "FUTURE"
  ],
  "LOT_SIZE": [
      268.0,
      268.0,
      268.0,
      268.0,
      500.0
  ]
};

const individualObjects = summaryTable.PRODUCT_CODE.map((productCode, index) => {
  return {
    product_code: productCode,
    type: summaryTable.TYPE[index].toLowerCase(),
    lot_size: summaryTable.LOT_SIZE[index]
  }
});

console.log(individualObjects);
英文:

You can use .map and create a new object for each element.

Here's an example:

const summaryTable = {
  "PRODUCT_CODE": [
      123,
      123,
      123,
      123,
      123
  ],
  "TYPE": [
      "CURRENT",
      "OPTIMAL",
      "MINIMUM",
      "MAXIMUM",
      "FUTURE"
  ],
  "LOT_SIZE": [
      268.0,
      268.0,
      268.0,
      268.0,
      500.0
  ]
};

const individualObjects = summaryTable.PRODUCT_CODE.map((productCode, index) => {
  return {
    product_code: productCode,
    type: summaryTable.TYPE[index].toLowerCase(),
    lot_size: summaryTable.LOT_SIZE[index]
  }
});

console.log(individualObjects);

答案2

得分: 1

你可以迭代table中的一个数组的所有值以获取索引,然后迭代table的键和值以获取每个属性。这样,如果table发生变化,你不需要修改这段代码。

const table = {
  PRODUCT_CODE: [123, 123, 123, 123, 123],
  TYPE: ["CURRENT", "OPTIMAL", "MINIMUM", "MAXIMUM", "FUTURE"],
  LOT_SIZE: [268.0, 268.0, 268.0, 268.0, 500.0],
};

const entries = Object.entries(table);
const data = table.TYPE.map((_, i) => entries.reduce((acc, [key, value]) => {
  acc[key] = value[i];
  if (key === "TYPE") acc[key] = value[i].toLowerCase(); // if you need this, keep it
  return acc;
}, {}));

console.log(data);
英文:

You can iterate over all the values of one of the arrays stored in table to be able to get the index, then iterate over the keys & values of table to get every property. This makes it so if table changes, you don't need to modify this code.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const table = {
  PRODUCT_CODE: [123, 123, 123, 123, 123],
  TYPE: [&quot;CURRENT&quot;, &quot;OPTIMAL&quot;, &quot;MINIMUM&quot;, &quot;MAXIMUM&quot;, &quot;FUTURE&quot;],
  LOT_SIZE: [268.0, 268.0, 268.0, 268.0, 500.0],
};

const entries = Object.entries(table);
const data = table.TYPE.map((_, i) =&gt; entries.reduce((acc, [key, value]) =&gt; {
  acc[key] = value[i];
  if (key === &quot;TYPE&quot;) acc[key] = value[i].toLowerCase(); // if you need this, keep it
  return acc;
}, {}));

console.log(data);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月10日 16:24:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75408535.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定