Skip to content

Instantly share code, notes, and snippets.

@kobitoDevelopment
Created March 21, 2025 06:41
Show Gist options
  • Save kobitoDevelopment/1093f8d0bd4a310477d06ad79b40c11e to your computer and use it in GitHub Desktop.
Save kobitoDevelopment/1093f8d0bd4a310477d06ad79b40c11e to your computer and use it in GitHub Desktop.
const sample = [
{
id: 1,
name: "name1",
},
{
id: 2,
name: "name2",
},
];
// mapを使ってidが1のnameをupdateに変更
const mapSample2 = sample.map((item) => {
if (item.id === 1) {
item.name = "update";
return item;
} else {
return item;
}
});
console.log(mapSample2);
// filterを使ってidが1オブジェクトを削除
const filterSample = sample.filter((item) => {
if (item.id === 1) {
return false;
} else {
return true;
}
});
console.log(filterSample);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment