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

191
Views
delete element from array without empty slots

I create an array like this and remove one element:

var test = []

test.push({
    bezeichnung: "test_1"
});
test.push({
    bezeichnung: "test_2"
});
test.push({
    bezeichnung: "test_3"
});

delete test[1]

But console.log(test.length) will show me "3", because my array shows an "empty slot" which will also count.

How can I fix this?

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

0

You can use .splice() to delete the data like this:

test.splice(1,1);

Then the test.length with show 2 as the array length.

If you do not wish to reindex the array, you can simply use the .filter() method to filter the undefined data from your array like this:

var test = []

test.push({
    bezeichnung: "test_1"
});
test.push({
    bezeichnung: "test_2"
});
test.push({
    bezeichnung: "test_3"
});

delete test[1];

console.log(test.length) //will log 3
console.log(test.filter(a=>a).length) //will log 2

The last console statement in the above code snippet would filter the empty/undefined values from the array and show you the count.

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!