Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

240
Visualizações
How to add a element in between array using const array in Javascript
  • How to add an element in array when I am using const array?
    const arr = [2,3,4,5];
    allData = arr[1].concat(9); 
  • Required output: allData = [2,9,3,4,5]
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

In order to add an item into the middle of an array you can use the .splie() function. You can achieve your desired affect by using the code:

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

To understand the use of .splice(). The first param is the starting point, so where we are entering the item into, the second param is how many items will be deleted and finally the last param is what is going to be inserted into the array.

Thanks! Hope this helps!

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use method SPLICE for arrays. Splice is a powerful method to remove or add at any position an element in an array.

const arr = [2,3,4,5];

ADD AN ELEMENT

add 9 as second element in our array

arr.splice(1, 0, 9)
  • 1 - index where we start, in our case index 1 = 3 (because in arrays index 0 it's first element in an array, index 1 it's second element etc...)
  • 0 - how many elements we want to remove, in our case we don't want to remove any, that's why we have 0 here
  • 9 - our element wich we want to add, it can be number, string etc

console.log(array) = [2,9,3,4,5]

REMOVE AN ELEMENT

const arr = [2,3,4,5];

remove the second element(3) and insert 9 instead

arr.splice(1, 1, 9)
  • first argument("1") means that we start at index 1 (so second element in the array).
  • second argument("1") means that we want to remove exactly one element.
  • third argument("9") means that we want to add element 9

console.log(arr) = [2,9,4,5]

I hope this helps you understand the method better.

about 4 years ago · Juan Pablo Isaza Relatório

0

You can an item in between array using .splice() method.

For further information regarding this you can check this link. [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice][1]

Hope this helps

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda