admin管理员组

文章数量:1336340

I'm deleting some array object in traditional way: delete subDirectories[index]. So, just after that this object is changing to [empty] one. Now, how to filter that, undefined, bool, NaN nothing works. I'm working with Vue.js and this contains an vuex action. Can anybody help?

I'm deleting some array object in traditional way: delete subDirectories[index]. So, just after that this object is changing to [empty] one. Now, how to filter that, undefined, bool, NaN nothing works. I'm working with Vue.js and this contains an vuex action. Can anybody help?

Share Improve this question asked Feb 6, 2018 at 0:45 LukasLukas 7,74420 gold badges79 silver badges127 bronze badges 5
  • check this stackoverflow./questions/281264/… – Gaurang Dave Commented Feb 6, 2018 at 0:47
  • I don't understand what you are saying. Are you sure this is javascript? – keithlee96 Commented Feb 6, 2018 at 0:48
  • @keithlee96 I've added the console log screen. – Lukas Commented Feb 6, 2018 at 0:49
  • @GaurangDave tried this, and just like above, none of them doesn't work :( – Lukas Commented Feb 6, 2018 at 0:49
  • @Lukas Can you share the JS code (operation you are performing)? – Gaurang Dave Commented Feb 6, 2018 at 1:10
Add a ment  | 

1 Answer 1

Reset to default 6

If you want to delete all null, undefined, (or any false-like) values in an array, you can just do:

var arr = [1,3,5, null, False];
var res = arr.filter(val=>val);
console.log(res); // [1,3,5]

Alternatively, you can explicitly remove null and undefined:

var res = arr.filter(val => (val!==undefined) && (val!==null));
console.log(res); // [1,3,5]

本文标签: javascriptFilter empty array objectStack Overflow