JavaScript: Manipulation des tableaux

sidali

Sid Ali BENTIFRAOUINE

Posted on October 4, 2022

JavaScript: Manipulation des tableaux

Le filter (ne fait pas le cafè)

let USERS = [
  { id: 1, name: "Paul" },
  { id: 2, name: "Jean-Karim" },
  { id: 3, name: "Jean-Babibel" },
]

// 1. Supprimer "Jean-Karim"
// id: 2
const id_du_user_a_supprimer = 2
USERS = USERS.filter(u => u.id != id_du_user_a_supprimer)
Enter fullscreen mode Exit fullscreen mode

Le map (ce n'est pas une carte)

// 2. Modifier "Jean-Karim"
// id: 2
const id_du_user_a_editer = 2
USERS = USERS.map((u) => {
  if (u.id != id_du_user_a_editer) {
    return u
  } else {
    return {
      ...u,
      name: "Jean-Baptiste",
    }
  }
})
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
sidali
Sid Ali BENTIFRAOUINE

Posted on October 4, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related