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

187
Views
jQuery reindex array

I have this javascript / jQuery code:

var json = [
    {
        id: 0,
        text: 'enhancement'
    },
    {
        id: 1,
        text: 'bug'
    },
    {
        id: 3,
        text: 'invalid'
    },
    {
        id: 4,
        text: 'wontfix'
    }
];



delete json[2]
console.log(json)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

This code delete the array key 2. But I need an reindex after that, so that I can access the other 3 values like:

json[0]
json[1]
json[2]

How can I realize this ?

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

0

Use splice instead of delete like below (W3schools splice):

json.splice(target_index,1);

Please read the Mozila page about the splice method for more information.

about 4 years ago · Juan Pablo Isaza Report

0

If you don't want to mutate the value you can simply filter the array

json.filter((i, idx) => idx !== 2)
about 4 years ago · Juan Pablo Isaza Report

0

you can reindex by simply using

json.filter(function(){return true;})

here is a working demo.

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("#delete").click(function(){
   var json = [
    {
        id: 0,
        text: 'enhancement'
    },
    {
        id: 1,
        text: 'bug'
    },
    {
        id: 3,
        text: 'invalid'
    },
    {
        id: 4,
        text: 'wontfix'
    }
];



delete json[2]


console.log(json.filter(function(){return true;}))
  });
});
</script>
</head>
<body>
 <button id="delete">delete</button>
</body>
</html>

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!