JS数组中的字段匹配成新的字符串数组
此代码存在于特定的环境: 数组最大长度为4。
此代码也不是最优,待优化。
const DEFAULT_A = [
{
name: '1',
content: [
{
name: '1-1',
code: '1-1',
},
{
name: '1-2',
code: '1-2',
},
],
},
{
name: '2',
content: [
{
name: '2-1',
code: '2-1',
},
{
name: '2-2',
code: '2-2',
},
{
name: '2-3',
code: '2-3',
},
],
},
{
name: '3',
content: [
{
name: '3-1',
code: '3-1',
},
{
name: '3-2',
code: '3-2',
},
{
name: '3-3',
code: '3-3',
},
],
},
{
name: '4',
content: [
{
name: '4-1',
code: '4-1',
},
{
name: '4-2',
code: '4-2',
},
{
name: '4-3',
code: '4-3',
},
],
},
];
// 准备一个空数组,用来存放匹配好的数据
const finalArr: any = [];
const maxLength = DEFAULT_A.length;
DEFAULT_A.forEach((item, index) => {
const isEnd = DEFAULT_A.length - 1 === index;
item.content.forEach(firstItem => {
if (maxLength === 1) {
finalArr.push(firstItem.code);
} else {
DEFAULT_A[isEnd ? 0 : index + 1].content.forEach(secondItem => {
if (maxLength === 2) {
finalArr.push(`${firstItem.code}_${secondItem.code}`);
finalArr.push(`${secondItem.code}_${firstItem.code}`);
} else if (maxLength === 3) {
DEFAULT_A[index === 0 ? 2 : (index === 1 ? 0 : 1)].content.forEach(thirdItem => {
const A = `${firstItem.code}_${secondItem.code}_${thirdItem.code}`;
const B = `${firstItem.code}_${thirdItem.code}_${secondItem.code}`;
const C = `${secondItem.code}_${firstItem.code}_${thirdItem.code}`;
const D = `${secondItem.code}_${thirdItem.code}_${firstItem.code}`;
const E = `${thirdItem.code}_${firstItem.code}_${secondItem.code}`;
const F = `${thirdItem.code}_${secondItem.code}_${firstItem.code}`;
[A, B, C, D, E, F].forEach(alreadyItem => {
if (!finalArr.includes(alreadyItem)) {
finalArr.push(alreadyItem);
}
});
});
} else if (maxLength === 4) {
DEFAULT_A[index === 0 ? 2 : (index === 1 ? 3 : (index === 2 ? 0 : 1))].content.forEach(thirdItem => {
DEFAULT_A[index === 0 ? 3 : (index === 1 ? 0 : (index === 2 ? 1 : 2))].content.forEach(fourthItem => {
const A = `${firstItem.code}_${secondItem.code}_${thirdItem.code}_${fourthItem.code}`;
const B = `${firstItem.code}_${secondItem.code}_${fourthItem.code}_${thirdItem.code}`;
const C = `${firstItem.code}_${thirdItem.code}_${secondItem.code}_${fourthItem.code}`;
const D = `${firstItem.code}_${thirdItem.code}_${fourthItem.code}_${secondItem.code}`;
const E = `${firstItem.code}_${fourthItem.code}_${secondItem.code}_${thirdItem.code}`;
const F = `${firstItem.code}_${fourthItem.code}_${thirdItem.code}_${secondItem.code}`;
const G = `${secondItem.code}_${firstItem.code}_${thirdItem.code}_${fourthItem.code}`;
const H = `${secondItem.code}_${firstItem.code}_${fourthItem.code}_${thirdItem.code}`;
const I = `${secondItem.code}_${thirdItem.code}_${firstItem.code}_${fourthItem.code}`;
const J = `${secondItem.code}_${thirdItem.code}_${fourthItem.code}_${firstItem.code}`;
const K = `${secondItem.code}_${fourthItem.code}_${firstItem.code}_${thirdItem.code}`;
const L = `${secondItem.code}_${fourthItem.code}_${thirdItem.code}_${firstItem.code}`;
const M = `${thirdItem.code}_${firstItem.code}_${secondItem.code}_${fourthItem.code}`;
const N = `${thirdItem.code}_${firstItem.code}_${fourthItem.code}_${secondItem.code}`;
const O = `${thirdItem.code}_${secondItem.code}_${firstItem.code}_${fourthItem.code}`;
const P = `${thirdItem.code}_${secondItem.code}_${fourthItem.code}_${firstItem.code}`;
const Q = `${thirdItem.code}_${fourthItem.code}_${firstItem.code}_${secondItem.code}`;
const R = `${thirdItem.code}_${fourthItem.code}_${secondItem.code}_${firstItem.code}`;
const S = `${fourthItem.code}_${firstItem.code}_${secondItem.code}_${thirdItem.code}`;
const T = `${fourthItem.code}_${firstItem.code}_${thirdItem.code}_${secondItem.code}`;
const U = `${fourthItem.code}_${secondItem.code}_${firstItem.code}_${thirdItem.code}`;
const V = `${fourthItem.code}_${secondItem.code}_${thirdItem.code}_${firstItem.code}`;
const W = `${fourthItem.code}_${thirdItem.code}_${firstItem.code}_${secondItem.code}`;
const X = `${fourthItem.code}_${thirdItem.code}_${secondItem.code}_${firstItem.code}`;
[
A, B, C, D, E, F,
G, H, I, J, K, L,
M, N, O, P, Q, R,
S, T, U, V, W, X,
].forEach(alreadyItem => {
if (!finalArr.includes(alreadyItem)) {
finalArr.push(alreadyItem);
}
});
});
});
}
});
}
});
});