next.js 为什么.push()方法给出重复项

xxhby3vn  于 2022-12-18  发布在  其他
关注(0)|答案(1)|浏览(124)
{
    category: 'Music',
    id: '625064c2e12f6dec240bdfcb',
    correctAnswer: "Don't Leave Me This Way",
    incorrectAnswers: [
      'Ice Ice Baby',
      'In-A-Gadda-Da-Vida',
      'Rebirth of Slick (Cool Like Dat)'
    ],
    question: 'What song did Thelma Houston have a hit with in 1976?',
    tags: [ 'songs', 'one_hit_wonders', 'music' ],
    type: 'Multiple Choice',
    difficulty: 'hard',
    regions: []
  }
// Here is the response from the API...
  
   const oldArray = quizData[number].incorrectAnswers;
  oldArray.push(quizData[number].correctAnswer);
  console.log(oldArray);
// Here i am pushing the correctAnswer into the incorrect Answer array but my console.log keeps repeating the correct answer data, Why is this happening? Am i pushing it wrong? 

  ['An arrow', 'A lightning bolt', 'A bottle of wine', 'A Discus', 'A Discus', 'A Discus', 'A Discus', 'A Discus']
//Here is my console.log array.
// 'A Discus' keeps getting repeated 4 times, Same length of the present array, So the array has 7 values instead of just 3, What is going on?
hc2pp10m

hc2pp10m1#

使用扩展运算符以避免重复

console.log( [...new Set(oldArray)])
const list =   ['Anemia', 'Heart Attack', 'Paralysis', 'Epilepsy', 'Epilepsy']

console.log( [...new Set(list)])

相关问题