score:1
that's simply not how startswith works. you can only supply it a single test string (and, optionally and rarely, a start position.)
try replacing
! prod.productid .startswith ('bgm', 's11', 'bgp')
with
! (['bgm', 's11', 'bgp'] .some (s => prod .productid .startswith (s)))
or equivalently,
(['bgm', 's11', 'bgp'] .every (s => ! prod .productid .startswith (s)))
depending on which makes more sense to you, and doing the same for your currentprice
. that may capture what you need.
score:1
string.prototype.startswith, cannot be passed three separate strings to check.
if your pseudo code is:
if product id does not begin with bgm, s11, or bgp
,
then you could test it with a regular expression or else use three separate statements like so:
let discountprice = (!prod.productid.startswith('bgm') &&
!prod.productid.startswith('s11') &&
!prod.productid.startswith('bgp') &&
prod.currentprice.tostring().endswith('97'))
? parsefloat(prod.currentprice) * 0.05
: !prod.productid.startswith('bgm', 's11', 'bgp')
? parsefloat(prod.currentprice) * 0.15
: 0
using a regex:
let discountprice = (!/^(s11|bgp|bgm)/.test(prod.productid) && prod.currentprice.tostring().endswith('97'))
? parsefloat(prod.currentprice) * 0.05
: !prod.productid.startswith('bgm', 's11', 'bgp')
? parsefloat(prod.currentprice) * 0.15
: 0
score:2
it is not possible to pass multiple options into startswith() method.
the method has following syntax: string.startswith(searchvalue, start)
you can try:
- checking startswith for each string separately:
!(prod.productid.startswith('bgm') || prod.productid.startswith('s11') || prod.productid.startswith('bgp'))
- looping through array of options:
!['bgm', 's11', 'bgp'].some(item => prod.productid.startswith(item))
- using regular expression:
!/^(bgm|s11|bgp)/.test(prod.productid)
Source: stackoverflow.com
Related Query
- startsWith only working for first condition
- bootstrap-select is used in multiple places in one page but only working for the first one
- For loop only working on second loop with React hooks
- event listener resize window not working on first expand only
- React.js + bootstrap-table working only on first load, but transitions break the table
- ES6 - Destructuring and For of Loop only returns first object
- SetInterval only run for first time
- React scroll to bottom only works for the first time (demo inside)
- JS Regex for first letters only, but if the word it only two letters keep both AND numbers as is
- onClick function only works for the first time in react?
- Jest manual mocking works only for the first function call
- How to do something only for the first item in aray
- search in reactjs only showing for the last condition but not getting results for above lines
- mapDispatchToProps dispatch action working for only one function
- Function invoking only for first item React
- React Redux Firebase - isLoaded only false for the first document when using firestore
- setState works only for first time in React js
- Why does this react form works only for the first time?
- React App only re-renders on first state change and changes are not visible for second change even though the change is successful
- Update a state variable only on loading the page for the first time in react js
- Reactjs form calculation data working only for inputs
- React browserhistory.push not working for the first time
- In Reactjs,is it possible to update the state of a component only for those items that satisfy a particular condition
- Why does only the first CSS class of my project working and not the rest
- Tailwind transition delay arbitrary value only working for specific values
- Map over TypeScript array of 2 different types with a condition for a property that only exists on one Type?
- React router V6 only working for root path
- Lodash sortby first condition and then check for other conditions
- my function for filtering isnt working properly it dosent register first change in input field
- Adding emoji is showing undefined when adding it for the first time only
More Query from same tag
- React test with Jest - onChange never called for Checkbox
- How do you update a component again straight after an update in react
- Why is not tree checkboxes checked in the child component using Ant Tree Design
- React, SpringBoot and CSRF
- How to load components conditionally in ReactJS
- React state is not being updated using useState()
- How to import from subdirectory from a node_module?
- Warning: Functions are not valid as a React child, using a conditional rendering
- React Query JSON-compatible values
- How to format a string fetch from API array into a mix of text and ordered list? (in react)
- React Hooks: Idiomatic way to ensure that useEffect runs only when the contents of an array argument to the custom hook change
- react-select dynamic dropdown with async options loaded as user is typing
- React - JSX conditional tags
- Antd - is there anyway to change background color of the Card title?
- reset form.fields in antd reactjs
- React state not updating onclick
- React & Axios - Dynamically add key and value to object
- Search Through Array, Return Results
- How to limit keyboard input to letters only
- React Router doesn't work, but no errors were detected
- How to make a custom hook with useState which can update multiple elements if state changes?
- react-day-picker custom input loses focus when onDayChange selects a day
- Screen Reader reading properly on Mac chrome but not on Windows Chrome React
- React doesn't display my resized picture. Why?
- How to render simple React component in MVC6 application, for dummies
- How to fix Auth driver [jwt] for guard [api] is not defined
- checkbox not displaying check mark correctly
- Upload image using react to active storage rails
- NavLink change button color only on click moment but not setting it up
- map function doesn't work in React component