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

215
Views
Removing subarray from array with splice

I am using splice method to remove a subarray from an array with the following code:


    for(let i = 0; i < cid.length; i++)
    {
      if(coinsValue[cid[i][0]] < troco && cid[i][1] > 0)
          available += cid[i][1]
      if(coinsValue[cid[i][0]] > troco)
          cid[i].splice(0)
    }


The output is :

[ [ 'PENNY', 1.01 ],
  [ 'NICKEL', 2.05 ],
  [ 'DIME', 3.1 ],
  [ 'QUARTER', 4.25 ],
  [ 'ONE', 90 ],
  [ 'FIVE', 55 ],
  [ 'TEN', 20 ],
  [ 'TWENTY', 60 ],
  [] /// removed elements from this array, but not the array itself ]

As you can see the elements that fit the condition are removed and I'm still left with an empty array. However I want to remove it completely!

Any help?

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

0

You're removing items from a sub array, not from the array itself.

To remove item from the top array you should do like this:

cid.splice(i, 1) instead of cid[i].splice(0)

for(let i = 0; i < cid.length; i++) {
    if (coinsValue[cid[i][0]] < troco && cid[i][1] > 0) {
        available += cid[i][1];
    } else if (coinsValue[cid[i][0]] > troco) {
        cid.splice(i, 1);
    }
}
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!