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

80
Views
Javascript version of list.pop

I am trying to create a javascript version of python's pop function.

Here is what I have so far:

function pop(arr, idx) {
  arr = arr.splice(0, idx).concat(arr.splice(idx + 1));
  console.log(arr);
}

I am not sure of why it is only returning the first part and not combining with the second.

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

0

are you looking for something like this? maybe you can modify it so that the pop function returns the removed element.

const arr1 =  [10,20,30,40,50,60];
const arr2 =  [10,20,30,40,50,60];
const arr3 =  [10,20,30,40,50,60];

function pop(arr, idx) {
    removedEl = arr.splice(idx,1)[0];
  console.log('removed element: ',removedEl)
  console.log('final array: ',arr);
}

pop(arr1,0);
pop(arr2,2);
pop(arr3,5)

about 4 years ago · Juan Pablo Isaza Report

0

Array.splice() modifies the array. It doesn't create a new array. All you need is this:

const pythonPop = ( arr , i = -1 ) => arr.splice(i,1)[0];
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!