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

183
Views
How to add a value to an array without the splice method and overwriting the element? javascript

Is there a way to concat an array at a specific index without overwriting the element, not using the splice method? I usually see concats happen in the beginning or end of an array.

Below is an example of using splice to concat an array at an index

var colors=["red","blue"];
var index=1;

//if i wanted to insert "white" at index 1
colors.splice(index, 0, "white");   //colors =  ["red", "white", "blue"]
```
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You could take Array#copyWithin with some adjustments.

const
    colors = ["red", "blue"],
    index = 1,
    value = "white";

colors.length++;                      // create space for moving
colors.copyWithin(index + 1, index);  // move values to right
colors[index] = value;                // add new value

console.log(colors); // ["red", "white", "blue"]

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!