我有以下数组:
[
["Polymeric", "Vehicle Graphics (Basic)"],
["Cast", "Vehicle Graphics (Part Wrap), Vehicle Graphics (Full Wrap)"],
["Polymeric", "Vehicle Graphics (Part Wrap)"]
]
我需要这个:
[
["Polymeric", "Vehicle Graphics (Basic)"],
["Cast", "Vehicle Graphics (Part Wrap)"],
["Cast", "Vehicle Graphics (Full Wrap)"],
["Polymeric", "Vehicle Graphics (Part Wrap)"]
]
这是我的代码:
const source = [
["Polymeric", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, "Vehicle Graphics (Basic)"],
["Cast", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, "Vehicle Graphics (Part Wrap), Vehicle Graphics (Full Wrap)"],
["Polymeric", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, "Vehicle Graphics (Part Wrap)"]
];
source.flatMap(([key, value1, value2, value3, value4, value5, value6, value7, value8, value9, value10, value11, value12, value13, value14, value15, value16]) => {
const values = value16.split(', ')
var hjk = values.map(singleValue => [key, singleValue])
console.log(hjk);
})
这是它工作的一部分。
有更好的方法吗?估价1、估价2等似乎很傻。
您可以破坏所需的值并映射所需的对。
const
source = [["Polymeric", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, "Vehicle Graphics (Basic)"], ["Cast", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, "Vehicle Graphics (Part Wrap), Vehicle Graphics (Full Wrap)"], ["Polymeric", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, "Vehicle Graphics (Part Wrap)"]],
result = source.flatMap(({ 0: key, 16: values }) => values
.split(', ')
.map(value => [key, value])
);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
您可以这样做:
const source = [
["Polymeric", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, "Vehicle Graphics (Basic)"],
["Cast", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, "Vehicle Graphics (Part Wrap), Vehicle Graphics (Full Wrap)"],
["Polymeric", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, "Vehicle Graphics (Part Wrap)"]
];
source.flatMap((arr) => {
let [key] = arr;
let value16 = arr[16];
const values = value16.split(', ')
var hjk = values.map(singleValue => [key, singleValue])
console.log(hjk);
})