如果对象值是数组,如何转换数组中的对象以创建它自己的倍数
How to transform object in array to create multiples of it's own if object value is an array
我有一个对象数组,它们具有相同的结构,除了一些值具有数组作为类型以表示它具有多个值。我需要根据数组中的项目数在该数组中创建一个新对象。
const arrayOfObjects = [
{
"Serial Number": "83675327350061093222581609820467",
"Part Number": "LJ98-10C779-AA",
"Cell Count": "32",
"Length": "6"
},
{
"Serial Number": "19584757350061093222581606739916",
"Type": "Extended Range",
"Part Number": "SW23",
"Length": "50"
},
{
"Serial Number": [
"90946117350061093222581604839920",
"77362117350061093222581604891733",
"77362117350061093222581604891734",
],
"Part Number": [
"TR40-60C779-AA",
"ST41-60C780-AA",
"ZT41-60C780-AA",
],
"Cell Count": [
"28",
"27",
"26"
],
"Length": [
"10",
"11",
"12"
]
}
]
下面是我想如何改造它
const transformedArray = [
{
"Serial Number": "83675327350061093222581609820467",
"Part Number": "LJ98-10C779-AA",
"Cell Count": "32",
"Length": "6"
},
{
"Serial Number": "19584757350061093222581606739916",
"Type": "Extended Range",
"Part Number": "SW23",
"Length": "50"
},
{
"Serial Number": "90946117350061093222581604839920",
"Part Number": "TR40-60C779-AA",
"Cell Count": "28",
"Length": "10"
},
{
"Serial Number": "77362117350061093222581604891733",
"Part Number": "ST41-60C780-AA",
"Cell Count": "27",
"Length": "11"
},
{
"Serial Number": "77362117350061093222581604891734",
"Part Number": "ZT41-60C780-AA",
"Cell Count": "26",
"Length": "12"
},
]
我已尝试通过以下代码尝试解决,请帮助完成:
let newItemsArray = [];
arrayOfObjects.map((item,index) => {
return Object.entries(item).map(([key,value]) => {
if(Array.isArray(value)){
value.forEach(x => {
newItemsArray.push({[key]:x})
})
}
})
})
您可以检查对象的条目是否包含数组并减少条目或将对象用于映射。
const
data = [{ "Serial Number": "83675327350061093222581609820467", "Part Number": "LJ98-10C779-AA", "Cell Count": "32", "Length": "6" }, { "Serial Number": "19584757350061093222581606739916", "Type": "Extended Range", "Part Number": "SW23", "Length": "50" }, { "Serial Number": [ "90946117350061093222581604839920", "77362117350061093222581604891733", "77362117350061093222581604891734"], "Part Number": [ "TR40-60C779-AA", "ST41-60C780-AA", "ZT41-60C780-AA"], "Cell Count": [ "28", "27", "26"], "Length": [ "10", "11", "12"] }],
result = data.flatMap(object => {
const entries = Object.entries(object);
return Array.isArray(entries[0][1])
? entries.reduce((r, [k, a]) => a.map((v, i) => ({ ...r[i], [k]: v })), [])
: object;
});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
没有flatMap
const
data = [{ "Serial Number": "83675327350061093222581609820467", "Part Number": "LJ98-10C779-AA", "Cell Count": "32", "Length": "6" }, { "Serial Number": "19584757350061093222581606739916", "Type": "Extended Range", "Part Number": "SW23", "Length": "50" }, { "Serial Number": [ "90946117350061093222581604839920", "77362117350061093222581604891733", "77362117350061093222581604891734"], "Part Number": [ "TR40-60C779-AA", "ST41-60C780-AA", "ZT41-60C780-AA"], "Cell Count": [ "28", "27", "26"], "Length": [ "10", "11", "12"] }],
result = data.reduce((array, object) => {
const entries = Object.entries(object);
if (Array.isArray(entries[0][1])) return [
...array,
entries.reduce((r, [k, a]) => a.map((v, i) => ({ ...r[i], [k]: v })), [])
];
return [...array, object];
}, []);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
您可以使用 Array#reduce
和 Object.keys
以及 Array#map
方法,如下所示:
const input = [
{
"Serial Number": "83675327350061093222581609820467",
"Part Number": "LJ98-10C779-AA",
"Cell Count": "32",
"Length": "6"
},
{
"Serial Number": "19584757350061093222581606739916",
"Type": "Extended Range",
"Part Number": "SW23",
"Length": "50"
},
{
"Serial Number": [
"90946117350061093222581604839920",
"77362117350061093222581604891733",
"77362117350061093222581604891734",
],
"Part Number": [
"TR40-60C779-AA",
"ST41-60C780-AA",
"ZT41-60C780-AA",
],
"Cell Count": [
"28",
"27",
"26"
],
"Length": [
"10",
"11",
"12"
]
}
];
const output = input.reduce((prev, {Length, ...rest}) =>
Array.isArray(Length) ?
prev.concat( Length.map((l,i) => Object.fromEntries( Object.keys(rest).map(k => [k,rest[k][i]]).concat( [["Length",l]] ))) ) :
prev.concat( {...rest,Length} ), []
);
console.log( output );
没有Object.fromEntries
const input = [
{
"Serial Number": "83675327350061093222581609820467",
"Part Number": "LJ98-10C779-AA",
"Cell Count": "32",
"Length": "6"
},
{
"Serial Number": "19584757350061093222581606739916",
"Type": "Extended Range",
"Part Number": "SW23",
"Length": "50"
},
{
"Serial Number": [
"90946117350061093222581604839920",
"77362117350061093222581604891733",
"77362117350061093222581604891734",
],
"Part Number": [
"TR40-60C779-AA",
"ST41-60C780-AA",
"ZT41-60C780-AA",
],
"Cell Count": [
"28",
"27",
"26"
],
"Length": [
"10",
"11",
"12"
]
}
];
const output = input.reduce((prev, {Length, ...rest}) =>
Array.isArray(Length) ?
prev.concat(
Length.map((l,i) =>
Object.keys(rest).map(k => ({[k]:rest[k][i]})).concat( {"Length":l} )
.reduce((p,c) => ({...p,...c}),{}) )
) :
prev.concat( {...rest,Length} ), []
);
console.log( output );
我有一个对象数组,它们具有相同的结构,除了一些值具有数组作为类型以表示它具有多个值。我需要根据数组中的项目数在该数组中创建一个新对象。
const arrayOfObjects = [
{
"Serial Number": "83675327350061093222581609820467",
"Part Number": "LJ98-10C779-AA",
"Cell Count": "32",
"Length": "6"
},
{
"Serial Number": "19584757350061093222581606739916",
"Type": "Extended Range",
"Part Number": "SW23",
"Length": "50"
},
{
"Serial Number": [
"90946117350061093222581604839920",
"77362117350061093222581604891733",
"77362117350061093222581604891734",
],
"Part Number": [
"TR40-60C779-AA",
"ST41-60C780-AA",
"ZT41-60C780-AA",
],
"Cell Count": [
"28",
"27",
"26"
],
"Length": [
"10",
"11",
"12"
]
}
]
下面是我想如何改造它
const transformedArray = [
{
"Serial Number": "83675327350061093222581609820467",
"Part Number": "LJ98-10C779-AA",
"Cell Count": "32",
"Length": "6"
},
{
"Serial Number": "19584757350061093222581606739916",
"Type": "Extended Range",
"Part Number": "SW23",
"Length": "50"
},
{
"Serial Number": "90946117350061093222581604839920",
"Part Number": "TR40-60C779-AA",
"Cell Count": "28",
"Length": "10"
},
{
"Serial Number": "77362117350061093222581604891733",
"Part Number": "ST41-60C780-AA",
"Cell Count": "27",
"Length": "11"
},
{
"Serial Number": "77362117350061093222581604891734",
"Part Number": "ZT41-60C780-AA",
"Cell Count": "26",
"Length": "12"
},
]
我已尝试通过以下代码尝试解决,请帮助完成:
let newItemsArray = [];
arrayOfObjects.map((item,index) => {
return Object.entries(item).map(([key,value]) => {
if(Array.isArray(value)){
value.forEach(x => {
newItemsArray.push({[key]:x})
})
}
})
})
您可以检查对象的条目是否包含数组并减少条目或将对象用于映射。
const
data = [{ "Serial Number": "83675327350061093222581609820467", "Part Number": "LJ98-10C779-AA", "Cell Count": "32", "Length": "6" }, { "Serial Number": "19584757350061093222581606739916", "Type": "Extended Range", "Part Number": "SW23", "Length": "50" }, { "Serial Number": [ "90946117350061093222581604839920", "77362117350061093222581604891733", "77362117350061093222581604891734"], "Part Number": [ "TR40-60C779-AA", "ST41-60C780-AA", "ZT41-60C780-AA"], "Cell Count": [ "28", "27", "26"], "Length": [ "10", "11", "12"] }],
result = data.flatMap(object => {
const entries = Object.entries(object);
return Array.isArray(entries[0][1])
? entries.reduce((r, [k, a]) => a.map((v, i) => ({ ...r[i], [k]: v })), [])
: object;
});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
没有flatMap
const
data = [{ "Serial Number": "83675327350061093222581609820467", "Part Number": "LJ98-10C779-AA", "Cell Count": "32", "Length": "6" }, { "Serial Number": "19584757350061093222581606739916", "Type": "Extended Range", "Part Number": "SW23", "Length": "50" }, { "Serial Number": [ "90946117350061093222581604839920", "77362117350061093222581604891733", "77362117350061093222581604891734"], "Part Number": [ "TR40-60C779-AA", "ST41-60C780-AA", "ZT41-60C780-AA"], "Cell Count": [ "28", "27", "26"], "Length": [ "10", "11", "12"] }],
result = data.reduce((array, object) => {
const entries = Object.entries(object);
if (Array.isArray(entries[0][1])) return [
...array,
entries.reduce((r, [k, a]) => a.map((v, i) => ({ ...r[i], [k]: v })), [])
];
return [...array, object];
}, []);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
您可以使用 Array#reduce
和 Object.keys
以及 Array#map
方法,如下所示:
const input = [
{
"Serial Number": "83675327350061093222581609820467",
"Part Number": "LJ98-10C779-AA",
"Cell Count": "32",
"Length": "6"
},
{
"Serial Number": "19584757350061093222581606739916",
"Type": "Extended Range",
"Part Number": "SW23",
"Length": "50"
},
{
"Serial Number": [
"90946117350061093222581604839920",
"77362117350061093222581604891733",
"77362117350061093222581604891734",
],
"Part Number": [
"TR40-60C779-AA",
"ST41-60C780-AA",
"ZT41-60C780-AA",
],
"Cell Count": [
"28",
"27",
"26"
],
"Length": [
"10",
"11",
"12"
]
}
];
const output = input.reduce((prev, {Length, ...rest}) =>
Array.isArray(Length) ?
prev.concat( Length.map((l,i) => Object.fromEntries( Object.keys(rest).map(k => [k,rest[k][i]]).concat( [["Length",l]] ))) ) :
prev.concat( {...rest,Length} ), []
);
console.log( output );
没有Object.fromEntries
const input = [
{
"Serial Number": "83675327350061093222581609820467",
"Part Number": "LJ98-10C779-AA",
"Cell Count": "32",
"Length": "6"
},
{
"Serial Number": "19584757350061093222581606739916",
"Type": "Extended Range",
"Part Number": "SW23",
"Length": "50"
},
{
"Serial Number": [
"90946117350061093222581604839920",
"77362117350061093222581604891733",
"77362117350061093222581604891734",
],
"Part Number": [
"TR40-60C779-AA",
"ST41-60C780-AA",
"ZT41-60C780-AA",
],
"Cell Count": [
"28",
"27",
"26"
],
"Length": [
"10",
"11",
"12"
]
}
];
const output = input.reduce((prev, {Length, ...rest}) =>
Array.isArray(Length) ?
prev.concat(
Length.map((l,i) =>
Object.keys(rest).map(k => ({[k]:rest[k][i]})).concat( {"Length":l} )
.reduce((p,c) => ({...p,...c}),{}) )
) :
prev.concat( {...rest,Length} ), []
);
console.log( output );