I am trying to write a function that prints only the even numbers from a nested array. Here is my attempt and it keeps returning "undefined".
我正在尝试编写一个函数,只打印嵌套数组中的偶数。这是我的尝试,它不断返回“未定义”。
function printEvents(){
var nestedArr = [[1,2,3],[4,5,6],[7,8],[9,10,11,12]];
for (var i = 0; i<nestedArr.length; i++) {
for (var j = 0; j<nestedArr[i]; j++) {
var evenNumbers = nestedArr[i][j]
}
}
if (evenNumbers % 2 == 0) {
console.log(evenNumbers)
}
}
printEvents();
4 个解决方案
#1
3
You could use a recursive approach, if the item is an array. You need to move the test for evenness inside of the for
loop.
如果项是数组,则可以使用递归方法。您需要在for循环内移动测试以获得均匀度。
function printEvents(array) {
var i;
for (i = 0; i < array.length; i++) {
if (Array.isArray(array[i])) {
printEvents(array[i]);
continue;
}
if (array[i] % 2 == 0) {
console.log(array[i]);
}
}
}
printEvents([[1, 2, 3], [4, 5, 6], [7, 8], [9, 10, 11, 12], [[[13, [14]]]]]);
Solution with a callback
带回调的解决方案
function getEven(a) {
if (Array.isArray(a)) {
a.forEach(getEven);
return;
}
if (a % 2 == 0) {
console.log(a);
}
}
getEven([[1, 2, 3], [4, 5, 6], [7, 8], [9, 10, 11, 12], [[[13, [14]]]]]);
#2
2
You just have a couple issues. You need to check the length of your nested arrays and you need to move your code that checks whether a number is even or not inside the array.
你只有几个问题。您需要检查嵌套数组的长度,并且需要移动代码来检查数组中数字是否均匀。
var nestedArr = [[1,2,3],[4,5,6],[7,8],[9,10,11,12]];
for (var i = 0; i<nestedArr.length; i++) {
for (var j = 0; j<nestedArr[i].length; j++) {
var evenNumbers = nestedArr[i][j]
if (evenNumbers % 2 == 0) {
console.log(evenNumbers)
}
}
}
#3
1
You can do this more easy using filter
method which accepts a callback
method.
使用接受回调方法的filter方法可以更轻松地完成此操作。
var nestedArr = [[1,2,3],[4,5,6],[7,8],[9,10,11,12]];
var mergedArray=[].concat.apply([], nestedArr);
console.log(mergedArray.filter(a=>a%2==0));
#4
0
You can use reduce with every for something quick.
您可以快速使用reduce with every。
var nestedArr = [ [1, 2, 3],[4, 5, 6],[7, 8],[9, 10, 11, 12]];
var sift = nestedArr.reduce(function(r,o) {
o.every(i => i % 2 === 0 ? r.push(i) : true)
return r;
}, []);
console.log(sift);
If you want a one-liner you can use ReduceRight with Filter
如果你想要一个单行,你可以使用ReduceRight和Filter
var nestedArr = [[1, 2, 3],[4, 5, 6],[7, 8],[9, 10, 11, 12]];
var sift = nestedArr.reduceRight((p, b) => p.concat(b).filter(x => x % 2 === 0), []);
console.log(sift)
#1
3
You could use a recursive approach, if the item is an array. You need to move the test for evenness inside of the for
loop.
如果项是数组,则可以使用递归方法。您需要在for循环内移动测试以获得均匀度。
function printEvents(array) {
var i;
for (i = 0; i < array.length; i++) {
if (Array.isArray(array[i])) {
printEvents(array[i]);
continue;
}
if (array[i] % 2 == 0) {
console.log(array[i]);
}
}
}
printEvents([[1, 2, 3], [4, 5, 6], [7, 8], [9, 10, 11, 12], [[[13, [14]]]]]);
Solution with a callback
带回调的解决方案
function getEven(a) {
if (Array.isArray(a)) {
a.forEach(getEven);
return;
}
if (a % 2 == 0) {
console.log(a);
}
}
getEven([[1, 2, 3], [4, 5, 6], [7, 8], [9, 10, 11, 12], [[[13, [14]]]]]);
#2
2
You just have a couple issues. You need to check the length of your nested arrays and you need to move your code that checks whether a number is even or not inside the array.
你只有几个问题。您需要检查嵌套数组的长度,并且需要移动代码来检查数组中数字是否均匀。
var nestedArr = [[1,2,3],[4,5,6],[7,8],[9,10,11,12]];
for (var i = 0; i<nestedArr.length; i++) {
for (var j = 0; j<nestedArr[i].length; j++) {
var evenNumbers = nestedArr[i][j]
if (evenNumbers % 2 == 0) {
console.log(evenNumbers)
}
}
}
#3
1
You can do this more easy using filter
method which accepts a callback
method.
使用接受回调方法的filter方法可以更轻松地完成此操作。
var nestedArr = [[1,2,3],[4,5,6],[7,8],[9,10,11,12]];
var mergedArray=[].concat.apply([], nestedArr);
console.log(mergedArray.filter(a=>a%2==0));
#4
0
You can use reduce with every for something quick.
您可以快速使用reduce with every。
var nestedArr = [ [1, 2, 3],[4, 5, 6],[7, 8],[9, 10, 11, 12]];
var sift = nestedArr.reduce(function(r,o) {
o.every(i => i % 2 === 0 ? r.push(i) : true)
return r;
}, []);
console.log(sift);
If you want a one-liner you can use ReduceRight with Filter
如果你想要一个单行,你可以使用ReduceRight和Filter
var nestedArr = [[1, 2, 3],[4, 5, 6],[7, 8],[9, 10, 11, 12]];
var sift = nestedArr.reduceRight((p, b) => p.concat(b).filter(x => x % 2 === 0), []);
console.log(sift)