过滤数组

过滤数组

过滤数组,生成新数组

javascript/jQueryboloog 发表了文章 • 1 个评论 • 1079 次浏览 • 2017-04-24 20:02 • 来自相关话题

过滤数组,只保留正数,直接在原数组上操作
题目:var arr = [3,1,0,-1,-3,2,-5]
function filter(arr){
// code...
}
filter(arr)
console.log(arr) // [3,1,2]解答:var arr = [3,1,0,-1,-3,2,-5];
function filter(arr){
for(var i = 0; i< arr.length; i++){
if(arr[i] < 0){
arr.splice(i--,1); // 当前索引值截取出来
}
}
}
filter(arr);
console.log(arr); // [3,1,2]
[/i]
?过滤数组,只保留正数,原数组不变,生成新数组
题目:var arr = [3,1,0,-1,-3,2,-5]
function filter(arr){
// code...
}
var arr2 = filter(arr)
console.log(arr2) // [3,1,2]
console.log(arr) // [3,1,0,-1,-2,2,-5]解答:[i]var arr = [3,1,0,-1,-3,2,-5]
function filter(arr){
var newArr = ;
for (var i = 0; i < arr.length; i++) {
if(arr[i] > 0){
newArr.push(arr[i]);
}
}
return newArr;
}
var arr2 = filter(arr);

console.log(arr2) // [3,1,2]
console.log(arr) // [3,1,0,-1,-2,2,-5][/i][/i][/i] 查看全部
过滤数组,只保留正数,直接在原数组上操作
题目:
var arr = [3,1,0,-1,-3,2,-5]
function filter(arr){
// code...
}
filter(arr)
console.log(arr) // [3,1,2]
解答:
var arr = [3,1,0,-1,-3,2,-5];
function filter(arr){
for(var i = 0; i< arr.length; i++){
if(arr[i] < 0){
arr.splice(i--,1); // 当前索引值截取出来
}
}
}
filter(arr);
console.log(arr); // [3,1,2]
[/i]

?过滤数组,只保留正数,原数组不变,生成新数组
题目:
var arr = [3,1,0,-1,-3,2,-5]
function filter(arr){
// code...
}
var arr2 = filter(arr)
console.log(arr2) // [3,1,2]
console.log(arr) // [3,1,0,-1,-2,2,-5]
解答:
[i]var arr = [3,1,0,-1,-3,2,-5]
function filter(arr){
var newArr = ;
for (var i = 0; i < arr.length; i++) {
if(arr[i] > 0){
newArr.push(arr[i]);
}
}
return newArr;
}
var arr2 = filter(arr);

console.log(arr2) // [3,1,2]
console.log(arr) // [3,1,0,-1,-2,2,-5][/i][/i][/i]

过滤数组,生成新数组

javascript/jQueryboloog 发表了文章 • 1 个评论 • 1079 次浏览 • 2017-04-24 20:02 • 来自相关话题

过滤数组,只保留正数,直接在原数组上操作
题目:var arr = [3,1,0,-1,-3,2,-5]
function filter(arr){
// code...
}
filter(arr)
console.log(arr) // [3,1,2]解答:var arr = [3,1,0,-1,-3,2,-5];
function filter(arr){
for(var i = 0; i< arr.length; i++){
if(arr[i] < 0){
arr.splice(i--,1); // 当前索引值截取出来
}
}
}
filter(arr);
console.log(arr); // [3,1,2]
[/i]
?过滤数组,只保留正数,原数组不变,生成新数组
题目:var arr = [3,1,0,-1,-3,2,-5]
function filter(arr){
// code...
}
var arr2 = filter(arr)
console.log(arr2) // [3,1,2]
console.log(arr) // [3,1,0,-1,-2,2,-5]解答:[i]var arr = [3,1,0,-1,-3,2,-5]
function filter(arr){
var newArr = ;
for (var i = 0; i < arr.length; i++) {
if(arr[i] > 0){
newArr.push(arr[i]);
}
}
return newArr;
}
var arr2 = filter(arr);

console.log(arr2) // [3,1,2]
console.log(arr) // [3,1,0,-1,-2,2,-5][/i][/i][/i] 查看全部
过滤数组,只保留正数,直接在原数组上操作
题目:
var arr = [3,1,0,-1,-3,2,-5]
function filter(arr){
// code...
}
filter(arr)
console.log(arr) // [3,1,2]
解答:
var arr = [3,1,0,-1,-3,2,-5];
function filter(arr){
for(var i = 0; i< arr.length; i++){
if(arr[i] < 0){
arr.splice(i--,1); // 当前索引值截取出来
}
}
}
filter(arr);
console.log(arr); // [3,1,2]
[/i]

?过滤数组,只保留正数,原数组不变,生成新数组
题目:
var arr = [3,1,0,-1,-3,2,-5]
function filter(arr){
// code...
}
var arr2 = filter(arr)
console.log(arr2) // [3,1,2]
console.log(arr) // [3,1,0,-1,-2,2,-5]
解答:
[i]var arr = [3,1,0,-1,-3,2,-5]
function filter(arr){
var newArr = ;
for (var i = 0; i < arr.length; i++) {
if(arr[i] > 0){
newArr.push(arr[i]);
}
}
return newArr;
}
var arr2 = filter(arr);

console.log(arr2) // [3,1,2]
console.log(arr) // [3,1,0,-1,-2,2,-5][/i][/i][/i]