--- id: 587d7b7b367417b2b2512b15 title: Iterate Through All an Array's Items Using For Loops challengeType: 1 forumTopicId: 301161 localeTitle: 使用 For 循环迭代数组的所有项 --- ## Description
在进行与数组有关的编程时,我们有时需要遍历数组的所有元素来找出我们需要的元素,或者对数组执行特定的操作。JavaScript 提供了几个内置的方法,它们以不同的方式遍历数组来获得不同的结果(如every()forEach()map()等等)。而简单的for循环不仅能实现这些功能,而且相比之下也更灵活。 请看以下例子: ```js function greaterThanTen(arr) { let newArr = []; for (let i = 0; i < arr.length; i++) { if (arr[i] > 10) { newArr.push(arr[i]); } } return newArr; } greaterThanTen([2, 12, 8, 14, 80, 0, 1]); // 返回 [12, 14, 80] ``` 这个函数使用一个for循环来遍历一个数组,逐一对其中的元素进行测试。我们用这个方法简单地以编程的方式找出了数组中大于10的元素,并返回了一个包含这些元素的数组。
## Instructions
我们已经定义了一个filteredArray函数,它接受一个嵌套的数组参数arr以及一个elem参数,并要返回一个新数组。arr数组中的数组可能包含elem元素,也可能不包含。请修改该函数,用一个for循环来做筛选,使函数返回一个由arr中不包含elem的数组组成的新数组。
## Tests
```yml tests: - text: 'filteredArray([[10, 8, 3], [14, 6, 23], [3, 18, 6]], 18)应该返回[ [10, 8, 3], [14, 6, 23] ]' testString: assert.deepEqual(filteredArray([ [10, 8, 3], [14, 6, 23], [3, 18, 6] ], 18), [[10, 8, 3], [14, 6, 23]]); - text: 'filteredArray([ ["trumpets", 2], ["flutes", 4], ["saxophones", 2] ], 2)应返回[ ["flutes", 4] ]' testString: assert.deepEqual(filteredArray([ ['trumpets', 2], ['flutes', 4], ['saxophones', 2] ], 2), [['flutes', 4]]); - text: 'filteredArray([ ["amy", "beth", "sam"], ["dave", "sean", "peter"] ], "peter")应该返回[ ["amy", "beth", "sam"] ]' testString: assert.deepEqual(filteredArray([['amy', 'beth', 'sam'], ['dave', 'sean', 'peter']], 'peter'), [['amy', 'beth', 'sam']]); - text: 'filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3)应该返回[ ]' testString: assert.deepEqual(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3), []); - text: filteredArray函数应该使用for循环 testString: assert.notStrictEqual(filteredArray.toString().search(/for/), -1); ```
## Challenge Seed
```js function filteredArray(arr, elem) { let newArr = []; // change code below this line // change code above this line return newArr; } // change code here to test different cases: console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3)); ```
## Solution
```js function filteredArray(arr, elem) { let newArr = []; // change code below this line for (let i = 0; i