我正在将代码从TypeScript转换为PHP,到目前为止一切顺利。 在TypeScript中,我需要翻译一些oneliner,但我似乎没有把它弄好。
TypeScript代码是:
const InContent: string = subclass.Ins
.map((In: In) => In.OutId + In.OutIndex)
.reduce((a, b) => a + b, '');
现在我知道PHP 7具有array_map
,array_reduce
和array_column
等功能。
我认为也可以在PHP中创建这个TypeScript oneliner作为oneliner,但我看不出如何。
答案 0 :(得分:1)
此代码似乎将OutId
和OutIndex
加在一起(我只能假设它是一个数学加法,而不是字符串连接),然后将所有这些连接成一个字符串。所以:
join(array_map(function ($in) { return $in->outId + $in->outIndex; }, $ins))
// I'm assuming `$in` is an object here
即使在JS中,reduce
会更加简洁为map(...).join()
。