DEV Community

Sid Ali BENTIFRAOUINE
Sid Ali BENTIFRAOUINE

Posted on

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

Top comments (0)