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

153
Views
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 answers
Answer question

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 Report

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 Report

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 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!