Skip to content

Instantly share code, notes, and snippets.

@butackle
Created November 17, 2017 10:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save butackle/9c536186f6a5de32d785cbbd5fbbc2b0 to your computer and use it in GitHub Desktop.
Save butackle/9c536186f6a5de32d785cbbd5fbbc2b0 to your computer and use it in GitHub Desktop.
let categories;
const example = {
username: "user1",
categories: [
{
name: "namae1",
type: "type1"
},
{
name: "namae2",
type: "type2"
}
]
}
//nameの値を全て変更したanswer2を新規作成
categories = example.categories.map((obj) => Object.assign({}, obj, { name: "name" }));
const answer2 = Object.assign({}, example, { categories });
console.log(answer2);
//"namae1"の値のみを変更したanswer3を新規作成
categories = example.categories.map((obj) => {
if(obj.name !== "namae1") return obj;
return Object.assign({}, obj, { name: "name" });
});
const answer3 = Object.assign({}, example, { categories });
//example自体の値を変える
example.categories.forEach((obj) => obj.name = "name");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment