 # FreeCodeCampBeta -Iterate Through All an Array's Items Using For Loops - Question

FreeCodeCampBeta -Iterate Through All an Array's Items Using For Loops - Question
0

#1

Hello, is there a better way to do this challenge than mine:

``````
function filteredArray(arr, elem) {
let newArr = [];
let arrL = arr.length; //4

for( let k = 0; k < arrL; k++ ) {
for( let i = 0; i < arr[k].length; i++ ) {
if( arr[k][i] == elem ) {
arr[k].splice(0, arr[k].length);
}
}
}

for( let d = 0; d < arr.length; d++) {
if( arr[d].length > 0) {
newArr.push( arr[d] );
}
}
return newArr;

};

console.log(filteredArray([[10, 8, 3], [14, 6, 23], [3, 18, 6]], 18));
``````

And by better, i’m sure this part can be a lot better:

``````	for( let d = 0; d < arr.length; d++) {
if( arr[d].length > 0) {
newArr.push( arr[d] );
}
}
return newArr;
``````

I’m looking for some thing to delete empty Array in Array.

#2

If you don’t mind modifying the original array, you could modify your solution as:

``````function filteredArray(arr, elem) {
for( let k = 0; k < arr.length; k++ ) {
for( let i = 0; i < arr[k].length; i++ ) {
if( arr[k][i] == elem ) {
arr.splice(k--, 1);
break;
}
}
}
return arr;
}
``````

If you do not want to modify the original array:

``````function filteredArray(arr, elem) {
let newArr = [];
let arrL = arr.length; //4

for( let k = 0; k < arrL; k++ ) {
for( var i = 0; i < arr[k].length; i++ ) { // notice the change from let to var
if( arr[k][i] === elem )
break;
}
if (i === arr[k].length) // will add subarray if we iterated through without break
newArr.push(arr[k]);
}
return newArr;
}
``````

UPDATE: I edited the code of my first function above due to a fellow camper pointing out I had a mistake in my code. It now is correct.

split this topic #3

A post was split to a new topic: Can someone tell me why my code doesn’t work just on filteredArray

#5

Hi ;
This is my code . It is a shorter way with indexOf()

function filteredArray(arr, elem) {
let newArr = [];
// change code below this line
for( var i = 0 ; i<arr.length ; i++){
if (arr[i].indexOf(elem) === -1){
newArr.push(arr[i]);
}
}
// 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));

#6
``````function filteredArray(arr, elem) {
let newArr = [...arr];
// change code below this line
for (let i = 0; i < newArr.length; i++) {
for (let j = 0; j < newArr[i].length; j++) {
if (newArr[i][j] == elem) {
newArr.splice(i, 1);
i--;   // <---
break;
}
}
}
// change code above this line
return newArr;
}
// change code here to test different cases:
console.log(filteredArray([["trumpets", 2], ["flutes", 4], ["saxophones", 2]], 2));
// return [["flutes", 4]]
``````

#7

Great solution (even better considering that the indexOf method was a previous challenge).

#8
``````function filteredArray(arr, elem) {
let newArr = [];
// change code below this line
let contador = 0;
let longitud = 0;
for (let i = 0; i<arr.length; i++){
for (var j=0; j<arr[i].length;j++){
longitud++;
if (arr[i][j]==elem){
}
if (contador== 0 && longitud == arr[i].length ){
newArr.push(arr[i]);
}
}
longitud = 0;
}
// change code above this line
return newArr;
}

// change code here to test different cases:
console.log(filteredArray([ ["trumpets", 2], ["flutes", 4], ["saxophones", 2] ], 2));
``````

Really messy solution, but it works

#9

hey , can you please explain what the code do ?
i cant quite grasp how u r removing the array …

#10

Hi,
They’re testing each subarray to see if it contains elem using .indexOf() array method.
.indexOf(elem) returns -1 if the array does not contain elem
If the test comes back negative (does not contain elem), then that subarray gets pushed onto newArr.

``````
let arr = [10,50,75,"one"];
console.log(arr.indexOf(75);  // logs 2, for index 2
console.log(arr.indexOf("cow"); // logs -1, there is no index for cow
``````
``````function filteredArray(arr, elem) {
let newArr = [];

// loop through each subarray
for (var i = 0; i < arr.length; i++) {
// if subarray does not  contain elem, then push subarray onto newArr
if (arr[i].indexOf(elem) === -1) { // -1 indicates elem not found in arr
newArr.push(arr[i]);
}
}
return newArr;
}
``````

#11

Thanks for taking the time to reply!
i understand the logic now .
Many Thanks ^^

closed #12