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

150
Visualizações
How to copy a variable arrray from another arrray in javascripts

I have an array as below:

 const arr = [
        {
            title: 's4',
            value: '124'
        },
        {
            title: 's2',
            value: '121'
        },
        {
            title: 's3',
            value: '122'
        }
    ];

and I want to create a new another array copy from the old array same as below:

 const arrCopy = [
        {
            value: '124'
        },
        {
            value: '121'
        },
        {
            value: '122'
        }
    ];

then my code as below:

var arrCopy = [...arr,arr.value]

but it has a problem, so anyone help me, thanks.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Just as in the comment above you can use awesome Javascript functions, in this case, you would like to use the map function of your array to map every item of the array as you like.

const arrayMapped = yourArray.map(item => {
    value: item.value
  })

Here is another way using Javascript Destructuring, you just ask with properties would you like from the JS Object, in this case, you just like the value property.

  const arrayMapped = yourArray.map(( { value } ) => ( { value } ))

How Array.map works How Object Destructuring works

about 4 years ago · Juan Pablo Isaza Relatório

0

You can simply use Array.map, as it returns a new array with the required value.

const newArr = arr.map(element => ({ value: element.value }))
console.log(newArr);

For references : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

about 4 years ago · Juan Pablo Isaza Relatório

0

If you are allowed to import a library. Ramda has a lot of functions to work with arrays. For your specific question, project would do the job.

import R from "ramda";
R.project(["value"], arr)  //return equals arrCopy
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