var obj = [ { type: 'number' }, { type: 'string' }, { type: 'array', children: [ { type: 'number' }, { type: 'string' } ] }]var convert = function(obj) { return obj.map(o => ({ 'number': 1, 'string': 's', 'array': convert(o.children) }[o.type]))}var convert2 = function(obj) { return obj.map(o => { if (o.type === 'number') { return 1 } else if (o.type === 'string') { return 's' } else if (o.type === 'array') { return convert2(o.children) } else { return undefined } })}var converted = convert(obj)var converted2 = convert2(obj)
兩種遞歸的寫法,第一種為何報(bào)錯(cuò)?
搖曳的薔薇
2018-11-16 14:11:23