--- id: 587d7b7b367417b2b2512b15 title: Iterate Through All an Array's Items Using For Loops challengeType: 1 videoUrl: '' localeTitle: 使用for循环遍历所有数组的项目 --- ## Description
有时在使用数组时,能够遍历每个项目以查找我们可能需要的一个或多个元素,或者根据哪些数据项符合某组标准来操作数组非常方便。 JavaScript提供了几种内置方法,每种方法都以稍微不同的方式迭代数组以实现不同的结果(例如every()forEach()map()等),但是这种技术最灵活并且为我们提供了最大的控制量是一个简单的for循环。考虑以下:
function greaterThanTen(arr){
让newArr = [];
for(let i = 0; i <arr.length; i ++){
if(arr [i]> 10){
(ARR [I])newArr.push;
}
}
返回newArr;
}

greaterThanTen([2,12,8,14,80,0,1]);
//返回[12,14,80]
使用for循环,此函数遍历并访问数组的每个元素,并使其经受我们创建的简单测试。通过这种方式,我们可以轻松地以编程方式确定哪些数据项大于10 ,并返回包含这些项的新数组。
## Instructions
我们定义了一个函数filteredArray ,它接受arr ,一个嵌套数组和elem作为参数,并返回一个新数组。 elem表示在arr嵌套的一个或多个数组上可能存在或不存在的元素。使用for循环修改函数,以返回已传递数组的过滤版本,以便删除嵌套在包含elem arr中的任何数组。
## 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]], "filteredArray([[10, 8, 3], [14, 6, 23], [3, 18, 6]], 18) should return [ [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]], "filteredArray([ ["trumpets", 2], ["flutes", 4], ["saxophones", 2] ], 2) should return [ ["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"]], "filteredArray([ ["amy", "beth", "sam"], ["dave", "sean", "peter"] ], "peter") should return [ ["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), [], "filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3) should return [ ]");' - text: filteredArray函数应该使用for循环 testString: 'assert.notStrictEqual(filteredArray.toString().search(/for/), -1, "The filteredArray function should utilize a for loop");' ```
## 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 // solution required ```