Created
November 17, 2017 10:54
-
-
Save butackle/9c536186f6a5de32d785cbbd5fbbc2b0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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