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

292
Views
Splice() actually not working in javascript

Splice() actually not working in javascript!

const nums = ["1", "2", "3", "4", "5"];
 
nums.splice(0, 1);
nums.splice(1, 1);
nums.splice(2, 1);

console.log(nums);

But the output I want is this:

[
  "4",
  "5"
]

What sholud i do?

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

0

It is working exactly intended. Each time you splice, you change nums and index positions of each element also changes. You can see this if you console.log every time you splice. You can delete as many elements as you want at once if you just change the parameters a little bit.

const nums = ["1", "2", "3", "4", "5"];
 
nums.splice(0, 1);
console.log("splice(0,1)", nums);

nums.splice(1, 1);
console.log("splice(1,1)", nums);

nums.splice(2, 1);
console.log("splice(2,1)", nums);

const nums2 = ["1", "2", "3", "4", "5"];

nums2.splice(0,3);
console.log("splice(0,3)", nums2);
.as-console-wrapper { max-height: 100% !important; }

about 4 years ago · Juan Pablo Isaza Report

0

Splice() actually is working in javascript!

Its obvious you don't understand, every time you split the item gets removed and you get a new string what you want to do here is this:

const nums = ["1", "2", "3", "4", "5"];
 
nums.splice(0, 1);
nums.splice(0, 1);
nums.splice(0, 1);

console.log(nums);

Or this:

const nums = ["1", "2", "3", "4", "5"];
 
nums.splice(0, 3);

console.log(nums);
And the output you want is this:

[
  "4",
  "5"
]

This is what you should do.

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!