Ini kode saya, saya ingin membuat array yang difilter seperti contoh di bawah ini, saya memiliki 2 array dan ingin menambahkan informasi skor ke dalamnya, saya tahu ini sederhana tetapi tidak dapat menemukan solusinya

  const wishesData = [
    {
       name: "Peter",
      presents: ["coffee", "holidays"]
    },
    {
       name: "Mario",
      presents: ["coffee", "videogames"]
    },
    {
       name: "Amanda",
      presents: ["computer", "tattoo"]
    }
  ]
  const scoresData= [
    {
      name: "Peter",
      score: 10
    },
    {
      name: "Mario",
      score: 2.3
    },
    {
       name: "Amanda",
      score: 1.1
    }
  ]
  const result = wishesData.map((ele) => {
      return {
               ...ele,
              score:  scoresData.find(s=> s.name === ele.name? s.score: 0)
                  }
  })
           console.log("este es el resultado=>",result)

Saya ingin memodifikasi array wishData menambahkan "skor" ke semua objek di dalamnya dan terlihat seperti contoh ini:

{
  name: "Mario",
  presents: ["coffee", "videogames"],
  score: 2.3
}
2
foxDev 7 Januari 2021, 20:14

3 jawaban

Jawaban Terbaik

Silakan periksa contoh dan koreksi dan saran.

  • Koreksi: scoresData.find(s=> s.name === ele.name? s.score: 0) - di sini Anda tidak menutup tanda kurung untuk Array.find dan mencoba mengakses propertinya dalam find. Dalam kode Anda, Anda akan mendapatkan objek scoreData alih-alih score.
const match = scoresData.find(s=> s.name === ele.name); // -> ? s.score: 0)
return match ? match.score : 0;

// or simply
const score = scoresData.find(s=> s.name === ele.name)?.score || 0;
  • Saran: butuh waktu O(N^2). Semua iterasi untuk wishesData memerlukan iterasi lain scoresData untuk setiap iterasi. Mengapa Anda tidak menggunakan reduce yang diberikan dalam contoh?
const scoreMap = scoresData.reduce((a, c) => ({
    ...a,
    [c.name]: c.score
}), {})

// you can easy to find score by
const result = wishesData.map((ele) => {
    return {
        ...ele,
        score: scoreMap[ele.name] || 0,
    }
})

Terima kasih

const wishesData = [{
        name: "Peter",
        presents: ["coffee", "holidays"]
    },
    {
        name: "Mario",
        presents: ["coffee", "videogames"]
    },
    {
        name: "Amanda",
        presents: ["computer", "tattoo"]
    }
]
const scoresData = [{
        name: "Peter",
        score: 10
    },
    {
        name: "Mario",
        score: 2.3
    },
    {
        name: "Amanda",
        score: 1.1
    }
]
const scoreMap = scoresData.reduce((a, c) => ({
    ...a,
    [c.name]: c.score
}), {})
const result = wishesData.map((ele) => {
    return {
        ...ele,
        score: scoreMap[ele.name] || 0,
    }
})
console.log("este es el resultado=>", result)

Dan ini hanya mengedit kode asal Anda

const wishesData = [{
        name: "Peter",
        presents: ["coffee", "holidays"]
    },
    {
        name: "Mario",
        presents: ["coffee", "videogames"]
    },
    {
        name: "Amanda",
        presents: ["computer", "tattoo"]
    }
]
const scoresData = [{
        name: "Peter",
        score: 10
    },
    {
        name: "Mario",
        score: 2.3
    },
    {
        name: "Amanda",
        score: 1.1
    }
]
const result = wishesData.map((ele) => {
    return {
        ...ele,
        score: scoresData.find(s => s.name === ele.name)?.score || 0
    }
})
console.log("este es el resultado=>", result)
4
Alona 7 Januari 2021, 17:41
const wishesWithScores = wishesData.map(wishObject => {
  const descriptor = wishObject.name;
  const { score } = scoresData.find(({ name }) => name === descriptor);

  return {
    ...wishObject,
    score
  }
});
0
Sergey Lk 7 Januari 2021, 17:24

Anda mengembalikan seluruh objek, cukup kembalikan skornya:

const wishesData = [{
    name: "Peter",
    presents: ["coffee", "holidays"]
  },
  {
    name: "Mario",
    presents: ["coffee", "videogames"]
  },
  {
    name: "Amanda",
    presents: ["computer", "tattoo"]
  },
  {
    name: "Another",
    presents: ["computer", "tattoo"]
  }
]
const scoresData = [{
    name: "Peter",
    score: 10
  },
  {
    name: "Mario",
    score: 2.3
  },
  {
    name: "Amanda",
    score: 1.1
  }
]
const result = wishesData.map(ele => {
  const match = scoresData.find(s => s.name === ele.name)  
  return { ...ele, score: match ? match.score : 0  }
})
console.log("este es el resultado=>", result)
1
Anurag Srivastava 7 Januari 2021, 17:19