0

In nodejs, I have an array that looks somewhat like this.

["var/sys/log","var/sys/log/image", "var/sys/.dawn/noon", 
"var/sys/.sbs/rise", "var/sys/.iris/quell", "....." ]

The array index is around 50-70

Now I want to delete all the elements that contain a "." expect for the entries that contain .sbs.

Any help here would be great. I have been racking my head off.

Edit after comments

I have tried array.filter, array.splice(foo.indexOf('.'>-1). But it deletes everything including .sbs.

And if it has sbs in it, I shouldn't delete it

Runtime is Nodejs v8 and not a browser.

lonelymo
  • 3,972
  • 6
  • 28
  • 36
  • 3
    What have you tried? What methods did you think you might use? We prefer to help you figure out where you've gone wrong AFTER you show us what you've tried rather than just write the code for you from scratch. Hint, you might find the array method `.filter()` useful. – jfriend00 Sep 24 '15 at 23:30
  • note that .filter() is not supported in IE 8 – Paul Kienitz Sep 24 '15 at 23:32
  • 1
    What happens if a string contains both `.` and `.sbs` (at different places)? E.g. should `"foo.bar.sbs"` be deleted? – Oriol Sep 24 '15 at 23:33
  • @PaulKienitz - a [polyfill is available on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter#Polyfill) if IE8 support is still needed. There's little need to reinvent the world for this kind of stuff, even for old browsers. – jfriend00 Sep 24 '15 at 23:34
  • @PaulKienitz I think the OP wants to execute the JavaScript within the node.js environment, not a browser. – mvw Sep 24 '15 at 23:36
  • Updated. I have used filter and indexOf. It deletes everything with a . But I want to save entries that have .sbs – lonelymo Sep 24 '15 at 23:39
  • @Oriol it should be retained – lonelymo Sep 24 '15 at 23:41
  • check `if (item.indexOf('.sbs') !== -1)` first – Rodson Sep 24 '15 at 23:41

0 Answers0