我正在尝试使用 Javascript 将数组中的正数和负数分开。但是我尝试使用的代码不起作用。Jere 是我试图使用的代码,我从用 JavaScript 将数组中的负数和正数分开
var t = [-1,-2,-3,5,6,1]
var positiveArr = [];
var negativeArr = [];
t.forEach(function(item){
if(item<0){
negativeArr.push(item);
}
else{
positiveArr.push(item)
})
console.log(positiveArr) // should output [5, 6, 1]
console.log(negativeArr) // should output [-1, -2, -3]
您}
在条件中缺少一个右括号:
var t = [-1, -2, -3, 5, 6, 1];
var positiveArr = [];
var negativeArr = [];
t.forEach(function(item) {
if (item < 0) {
negativeArr.push(item);
} else {
positiveArr.push(item)
}
});
console.log(positiveArr) // should output [5, 6, 1]
console.log(negativeArr)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句