Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

201
Views
for loop: Nested array in javaScript

My code:

let newArr = [[1,5,6,4],[8,5,4],[4,4,4,4]];

function filterArr(arr, elemn){
  for(let i = 0; i < arr.length; i++){
    for(let j=0; j < arr[i].length; j++){
        if(arr[i][j] === elemn){
        arr[i].splice(j,1);
      }
    }
  }
  return arr;
}

console.log(filterArr(newArr,4));

The Result: [[1,5,6],[8,5],[4,4]]

I am stuck at this point : [4,4] it supposed []

any suggestion plz ...

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

I'll quote some sentences from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

deleteCount Optional
An integer indicating the number of elements in the array to remove from start.

If deleteCount is omitted, or if its value is equal to or larger than array.length - start (that is, if it is equal to or greater than the number of elements left in the array, starting at start), then all the elements from start to the end of the array will be deleted.

If deleteCount is 0 or negative, no elements are removed. In this case, you should specify at least one new element (see below).

You delete jth element and j++ so that the next of deleted element is skipped after deletion. After once deletion you should use j--.

The full right code is as follows:

let newArr = [[1,5,6,4],[8,5,4],[4,4,4,4]];

function filterArr(arr, elemn){
  for(let i = 0; i < arr.length; i++){
    for(let j=0; j < arr[i].length; j++){
        if(arr[i][j] === elemn){
        arr[i].splice(j, 1);
        j --;
      }
    }
  }
  return arr;
}

console.log(filterArr(newArr,4));

about 4 years ago · Juan Pablo Isaza Report

0

Global newArray.

let newArr = [[1,5,6,4],[8,5,4],[4,4,4,4]];

Here is filterArr function, parameter is same.

let result = [];
for (let item of newArr){
    for (let i = 0; i< item.length; i++){
        if (item[i] == elemn){
            item.splice(i, 1);
            i--;
        }
    }
     result.push(item);
}

return result;

You can call filterArr function and show same result as you expect.

console.log(filterArr(newArr,4));
about 4 years ago · Juan Pablo Isaza Report

0

As metioned in the comments, you might check out the javascript Array class. There is a built in filter method which might simplify this a bit.

let newArr = [[1,5,6,4],[8,5,4],[4,4,4,4]];

function filterArray(arr, valueToFilter) {
   return arr.map( (subArray) => subArray.filter( (value) => value !== valueToFilter ) );
}

console.log(filterArray(newArr,4));
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!