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

108
Views
Javascript duplicate array and change values

I have data like this :

const cards = [
  { name: "angular2" },
  { name: "vue" },
  { name: "react" },
  { name: "grunt" },
  { name: "phantomjs" }
];

I want to duplicate and shuffle this array. Also i want to add id property to each item.

const duplicateCards = (arr) => {
  const duplicatedArr = [...arr, ...arr];
  for (let i = duplicatedArr.length - 1; i >= 0; i--) {
    duplicatedArr[i].id = i;
    randomIndex = Math.floor(Math.random() * i + 1);
    console.log(duplicatedArr[i]);
    [duplicatedArr[i], duplicatedArr[randomIndex]] = [duplicatedArr[randomIndex], duplicatedArr[i]];
  }
  return duplicatedArr;
};

The duplicateCards(cards) function returns an output like this:

[
  { name: 'vue', id: 1 },
  { name: 'angular2', id: 0 },
  { name: 'vue', id: 1 },
  { name: 'grunt', id: 7 },
  { name: 'phantomjs', id: 4 },
  { name: 'phantomjs', id: 4 },
  { name: 'grunt', id: 7 },
  { name: 'react' },
  { name: 'react' },
  { name: 'angular2', id: 0 }
]

I tried concat() method but I got same result. I want to add a unique id. What is my mistake ? How can I solve this?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This happens because although you spread the array with [...arr, ...arr], creating a new array, the object references are still the same, so every object is now referenced twice. This means that any mutation you do (like with assigning to the id property) will be visible at two places in the array.

Secondly, you should assign the id property before the phase where you start swapping, as you may meet the same object a second time and then assign a different id to it, while other objects, which get swapped to the index i, will not get an id assigned.

Here is how it could work, by chaining a .map on the created array which creates new objects:

const duplicateCards = (arr) => {
  const duplicatedArr = [...arr, ...arr].map((o, id) => ({...o, id }));
  for (let i = duplicatedArr.length - 1; i >= 0; i--) {
    let randomIndex = Math.floor(Math.random() * i + 1);
    [duplicatedArr[i], duplicatedArr[randomIndex]] = [duplicatedArr[randomIndex], duplicatedArr[i]];
  }
  return duplicatedArr;
};

const cards = [
  { name: "angular2" },
  { name: "vue" },
  { name: "react" },
  { name: "grunt" },
  { name: "phantomjs" }
];

console.log(duplicateCards(cards));

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!