LoginSignup
0
2

More than 3 years have passed since last update.

配列の中に空objectがあったら削除する処理

Last updated at Posted at 2020-01-10

やりたかった事

今回やりたかった事は何かしらPOSTする時にエンドユーザーが必須項目でない入力欄を空で
submitした際に事前に配列の中身の空objectをdelete処理してPOSTする事。
因みに備忘録です。

実際の処理

実際にありそうなプロフィール入力だとこんな感じになるのかな?

hoge.component.ts
//入力された項目
const profile = {name : 'suzuki', age : '30', sex : null, address : null};
//この処理でnullだった場合、空objectを削除
Object.entries(profile).forEach( o => (o[1] === null ? delete profile[o[0]] : 0));
console.log(profile);
//{name: "suzuki", age: "30"}

まとめ

配列の中の空objectを削除したいってなった時にまぁ安易にif文使おうとしてたのでこういうやり方もあるんだなと勉強になった次第です。

0
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2