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

156
Views
function that takes a nested array and element as an argument, return a new array, if a nested array doesn't contain element it will push it to newArr

function filteredArray(arr, elem) {
  let newArr = [];
  for (let i = 0; i < arr.length; i++) {
    for (let j = 0; j < arr[i].length; i++) {
      if (arr[i][j] != elem) {
        newArr.push(arr[i]);
      }
    }
  }
  return newArr;
}

console.log(filteredArray([
  [3, 2, 3],
  [1, 6, 3],
  [3, 13, 26],
  [19, 3, 9]
], 3));

How should I remove that error? If a nested array doesn't contain element it will push it to newArr.

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

0

  1. You need to increment j in that inner loop.

  2. You also need to find a way to check to see if elem has been found, and only if it's been found, to add the nested array to the output.

function filteredArray(arr, elem) {
  let newArr = [];
  for (let i = 0; i < arr.length; i++) {
    let found = false;
    for (let j = 0; j < arr[i].length; j++) {
      if (arr[i][j] === elem) {
        found = true;
      }
    }
    if (!found) newArr.push(arr[i]);
  }
  return newArr;
}

console.log(JSON.stringify(filteredArray([
  [3, 2, 3],
  [1, 6, 3],
  [1, 6, 4],
  [3, 13, 26],
  [19, 3, 9]
], 3)));

A more modern method would be to filter out the arrays that where elem is not included:

function filteredArray(arr, elem) {
  return arr.filter(inner => {
    return !inner.includes(elem);
  });
}

console.log(JSON.stringify(filteredArray([
  [3, 2, 3],
  [1, 6, 3],
  [1, 6, 4],
  [3, 13, 26],
  [19, 3, 9]
], 3)));

about 4 years ago · Juan Pablo Isaza Report

0

function filteredArray(arr, elem) {
    let newArr = [];
    for (let i = 0; i < arr.length; i++) {
        for (let j = 0; j < arr[i].length; j++) {
            if (arr[i][j] == elem) {
                newArr.push(arr[i]);
            }
        }
    }
    return newArr;
}

console.log(filteredArray([
    [3, 2, 3],
    [1, 6, 2],
    [3, 13, 26],
    [19, 3, 9]
], 3));
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!