如何根据A数组将B数组组装为新数组?
let A = ['A', 'B', 'C']let B = [
['1A'],
['2A','2C']
]
根据 A 数组将 B 数组组装一个新数组
newArr = [ ['1A', null, null],
['2A', null, '2C']
]
我要的新数组
newArr = [ ['1A', null, null],
['2A', null, '2C']
]
回答:
const conv = (schema, source) => source.map(a => { const dict = {}
a.forEach(i => {
const name = i.at(-1) // 没有给出具体的数据格式,假定和 A 匹配的字符串就是最后一位
dict[name] = i
})
return schema.map(name => dict[name] ?? null)
})
回答:
let A = ['A', 'B', 'C'];let B = [
['1A'],
['2A','2C']
];
let newArr = B.map(subArr => A.map(letter => subArr.find(item => item.endsWith(letter)) || null));
console.log(newArr);
以上是 如何根据A数组将B数组组装为新数组? 的全部内容, 来源链接: utcz.com/p/935236.html