英文:
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: ["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);
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论