我有一个foreach
var a= [38, 34, 22, 19];
Array.forEach(function (b){
b= a+ b;
a.push(b);
});
但结果是
["38", "3834", "38,383422", "38,3834,38,38342219"]
如何阻止它们连接并产生
的结果["38, "72", "94", "119"]
答案 0 :(得分:2)
function createpath( $path=NULL, $perm=0644 ) {
if( !file_exists( $path ) ) {
createpath( dirname( $path ) );
mkdir( $path, $perm, TRUE );
clearstatcache();
}
return $path;
}
$root = $_SERVER['DOCUMENT_ROOT'];
chdir( $root );
chdir( '../' );
$uploads_dir = createpath( getcwd() . '/uploads/' );
会抛出错误。使用地图功能。希望关注代码段会很有用。
同样Array.forEach
将推送同一个数组
a.push(b);
答案 1 :(得分:1)
您也可以使用reduce
var a= [38, 34, 22, 19];
var output = a.reduce( (ac,c,i) => ( ac.push(ac.length == 0 ? c : c + ac[i-1] ) , ac ) , [] );
console.log(output);