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

228
Views
How do I loop through an array in javascript to get a new array whose length is not greater than 4?

I have this array whose length could be any number but not less than 4. How do I loop through this array to get a new array that has a length not greater than 4? I tried using the filter method but it returned an empty array.

This is my attempt:

// array could be of any length but not less than 4.
const projects = [
  'landing page',
  'portfolio page',
  'e-commerce app',
  'Dapps',
  'signup page',
  // ... even more
];

const newProjects = projects.filter((project, index, array) => {
  return project[index <= 4]
})

console.log(newProjects);

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

0

Corrected your approach;

const projects = ['landing page', 'portfolio page', 'e-commerce app', 'Dapps', 'signup page',.....] // array could be of any length but not less than 4

const newProjects = projects.filter((project, index, array) => {
  return index < 4;
})

console.log(newProjects);

Keep in mind that Array has zero-based index.

Proposed solution:

const newProjects = projects.slice(0,3);
about 4 years ago · Juan Pablo Isaza Report

0

You can simply use slice() for this:

const projects = ['landing page', 'portfolio page', 'e-commerce app', 'Dapps', 'signup page'];

console.log(projects.slice(0,4));

Slice returns a portion of an existing array without modifying it.

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!