const adFormats = { leaderboard: { sizes: [ [728, 90], ], }, rectangle: { sizes: [[320, 250], [300, 250], 'fluid'], }, halfpage: { sizes: [[300, 600], [320, 250], [300, 250]], },};这就是出错的地方Object.keys(adFormats).forEach(key => { adFormats[key].sizes.forEach(size => { if (typeof size !== 'string') { companionSizes += `${size[0]}x${size[1]}`; } companionSizes += '|'; }); companionSizes += ',';});// expected output: '728x90,320x250|300x250,300x600|320x250|300x250'我想用 join() 和/或 map() 使这个更干净。为了防止使用前导或尾随分隔符。还有一些更干净的代码。
1 回答
POPMUISE
TA贡献1765条经验 获得超5个赞
您可以过滤内部数组并连接所有嵌套并映射外部数组。
const
adFormats = { leaderboard: { sizes: [ [728, 90]] }, rectangle: { sizes: [[320, 250], [300, 250], 'fluid'] }, halfpage: { sizes: [[300, 600], [320, 250], [300, 250]] } },
result = ['leaderboard', 'rectangle', 'halfpage']
.map(k => adFormats[k].sizes
.filter(Array.isArray)
.map(a => a.join('x'))
.join('|')
)
.join(',')
console.log(result); // '728x90,320x250|300x250,300x600|320x250|300x250'
添加回答
举报
0/150
提交
取消