Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

set element at index javascript array and create new array

const items = [1, 2, 3, 4, 5]

const insert = (arr, index, newItem) => [
  // part of the array before the specified index
  ...arr.slice(0, index),
  // inserted item
  newItem,
  // part of the array after the specified index
  ...arr.slice(index)
]

const result = insert(items, 1, 10)

console.log(result)
// [1, 10, 2, 3, 4, 5]
Comment

PREVIOUS NEXT
Code Example
Javascript :: Javascript print/output 
Javascript :: api integration for web pages in next js 
Javascript :: for in loops javascript 
Javascript :: moment isbetween 
Javascript :: Find the maximum number of an array js 
Javascript :: create file node 
Javascript :: login js 
Javascript :: jquery sweet popup 
Javascript :: know when recyclerview is on the last item 
Javascript :: how to build a string javascript es6 
Javascript :: buffer image 
Javascript :: how to clear radio field in jquery 
Javascript :: react fontawesome exchange icon 
Javascript :: set className with ref react 
Javascript :: onClick={ (window.open react js 
Javascript :: css striped background 
Javascript :: browser support fetch api 
Javascript :: Generate random phone numbers in POSTMAN 
Javascript :: never give up 
Javascript :: set twig variable from javascript 
Javascript :: find longest word in a string javascript 
Javascript :: how to defined an array in js 
Javascript :: eaf doom emacs 
Javascript :: node js write read string to file 
Python :: python get appdata path 
Python :: tkinter always on top 
Python :: python use tqdm with concurrent futures 
Python :: python open web browser 
Python :: python get script name 
Python :: how to convert data type of a column in pandas 
ADD CONTENT
Topic
Content
Source link
Name
6+2 =