【JS】オブジェクトから特定のプロパティを除外する

const profile = {id: 1, name: 'hoge', age: 25, height: 170, weight: 65}

上記profileからidを除外したい場合、下記のようにすればできます。

const profile = {id: 1, name: 'hoge', age: 25, height: 170, weight: 65}
const {id, ...profileNoId} = profile

console.log(id) // 1
console.log(profileNoId) // { "name": "hoge", "age": 25, "height": 170, "weight": 65 } 

分割代入と残余引数を使うことで、profileNoIdにはid以外のプロパティが格納されます。

IT技術ブログ
↓↓「にほんブログ村」のランキングに参加しています。少しでも面白い、参考になったとか思われたらポチッとしていただけると嬉しいです!

にほんブログ村 IT技術ブログへ

にほんブログ村