score:0
i think it's more efficient to have your own function which does the same work.
const nth = function(val) {
var d = +val.tostring().split('').pop();
if (d > 3 && d < 21) return 'th';
switch (d % 10) {
case 1: return "st";
case 2: return "nd";
case 3: return "rd";
default: return "th";
}
}
var day = 23;
console.log(`${day}${nth(day)}`);
here the nth function takes the day as an argument and returns the day suffix as a string. so you can use it more flexibly. just simply pass the day into it and get your suffix as a string.
happy coding :)
score:1
here is the simple switch case solution:
switch (day) {
case 1: case 21: case 31:
getdaysuffix = 'st';
break;
case 2: case 22:
getdaysuffix = 'nd';
break;
case 3: case 23:
getdaysuffix = 'rd';
break;
default:
getdaysuffix = 'th';
}
score:2
instead of switch, you can use object lookups:
const getsuffix = day => {
const map = {
'1': 'st',
'21': 'st',
'31': 'st',
'2': 'nd',
'22': 'nd',
'3': 'rd',
'23': 'rd'
};
return map[day] || 'th';
}
but if you are looking for ordinal indicator conversion, you can use intl.pluralrules
to achieve that:
const pr = new intl.pluralrules('en-us', { type: 'ordinal' });
const map = {
other: 'th',
one: 'st',
two: 'nd',
few: 'rd',
};
const ordinal = num => num + map[pr.select(num)];
console.log(ordinal(1));
console.log(ordinal(2));
console.log(ordinal(3));
console.log(ordinal(13));
console.log(ordinal(24));
console.log(ordinal(42));
for the second function, you can convert the ternary expressions into if
else
:
const filter = selectedfilter[0].conditions.reduce((carry, current) => {
if (current.field === 'access_group_uuid') {
// eslint-disable-next-line in next line getting error at carry
if (!carry[current.field]) {
carry[current.field] = [];
}
carry[current.field] = carry[current.field].concat(current.value);
} else {
// eslint-disable-next-line in next line getting error at carry
if (carry[current.field]) {
carry[current.field] = [carry[current.field], current.value];
} else {
carry[current.field] = current.value;
}
}
return carry;
}, {});
Source: stackoverflow.com
Related Query
- how to convert this if else into switch case
- How can I remove this switch case when the React Element name is always the same as the switch variable?
- How can I convert Snake case into Start case?
- how to make this switch case statement less verbose?
- How to convert this into styled-components and how to nest classes
- Hey how do I convert this JS into react?
- how to convert this into a async function?
- How to convert if else shorthand to normal if else case
- How do I convert this function into a class component so that I can use render to call the return?
- how can I change my call condition into a switch case
- How do I convert this class component into function component?
- How can i convert this JSON object into this format?
- How would I convert this (file upload) class component into a functional component in React?
- How to convert image source into a JavaScript File object
- How to convert React class components into functional components and vice versa - IntelliJ
- How to convert String into a HTML element in Next JS Applcation?
- How to convert Date format like this Fri Apr 20 2020 00:00:00 GMT+0530 (India Standard Time) to 2020-04-20T00:00:00.000Z in Javascript?
- How to unit test switch case in React using Jest?
- how I convert string into interpolated string?
- How can I convert date/time into human readable form in react?
- How to convert server side Json data into react-google-chart format in Reactjs
- How to convert 2021-07-14T00:00:00.000Z into a standard date in YYYY-MM-DD format
- How to convert a React Class Component to a Function Component this project
- How does this arrow function work in my special case passing it through Redux
- How to take a comma separated array and convert it into individual list items? React.js & Javascript
- how should we convert string data into array and display into react table
- How do convert a datauri element into a file I can upload via react?
- Can I convert this into length all at once?
- How to convert a <table> from column into row when mapping a JSON object using React?
- How to save data as int using mongodb compass and koa js. Value automatically convert into String
More Query from same tag
- Component didn't re-renders on state change?
- Create an instance of a React class from a string
- How to map values to table in Javascript left to right
- ReactJS component state change rendered but then immediately revert back to initial state
- Handling Apollo errors on the component side
- React Router Dom, pass data to a component with useNavigate
- How to make svg fit container in React?
- How come I can't spread props with Typescript?
- Drop down not expanding on click because onChange won't fire
- What is the diffecence between these two if-else block and conditional operator block?
- Two click for dispatch action Redux
- React useState doesnt update from localstorage
- How do you test components that are wrapped with multiple higher order components?
- useRef hook for dynamic "onOutsideClick"?
- Dynamic variable call on GraphQL - React - Gatsby
- Why doesn't the Function see state updates?
- how to add type annotation for mock function
- alternative for this componentWillMount case?
- Next js api not works on netlify
- react-select package not working in build
- How to prevent a local state reset while dispatching action to reducer?
- React - How to get an id and use it to delete
- React.js onClick function to to render a table
- react/no-did-update-set-state linter rule alternative
- Runtime environment variable for react app
- react font awesome not working correctly in className property
- How to render emoji using Unicode in ReactJS?
- How to load third party script and initialise class in React component
- How to use the :first-of-type rule inside a styled-components/emotion partial?
- Redux-Form overwrite the previous value in React