0

I found this 'startsWith' Methode to select and filter 'keys' and then do something with the dataset in JavaScript,but now I look for a methode which is the opposite of

str.startsWith(searchString[, position])

For example, I only want to display datasets which doesn't have a key which starts with 'article'.

const data = Object.entries(y).filter(([key, value]) => (key.startsWith(!"article"))).map(([key, value]) => {}

the "!" Sign didn't work here. Does JavaScript offers a better Methode?

Thanks

R. Richards
  • 24,603
  • 10
  • 64
  • 64
teddy1995
  • 61
  • 1
  • 7
  • 2
    `!key.startsWith("article")` – Jeremy Thille Sep 18 '20 at 12:50
  • 1
    `key.startsWith(!"article")` is same as doing `key.startsWith("false")` – adiga Sep 18 '20 at 13:06
  • `!` is a logical operator, which evaluate your value and return a `boolean` [For more info: truthy-falsy](https://stackoverflow.com/questions/35642809/understanding-javascript-truthy-and-falsy#:~:text=In%20JavaScript%2C%20a%20truthy%20value,of%20falsy%20values%20in%20JavaScript%3A&text=undefined,-0) – Sheelpriy Sep 18 '20 at 13:10

1 Answers1

2

This doesn't make sense:

!"article"

You don't want to negate the value of the string, you want to negate the value retuned by .startsWith(). Semantically you want "not starts with" ("doesn't start with"):

!key.startsWith("article")
David
  • 208,112
  • 36
  • 198
  • 279