score:1
Accepted answer
Use of Set
instead of Array
Set
will automatically make values uniques. You could have something like :
// Declaration of the Set
const uniqueSet = new Set();
function handleClicked(calendarIconId) {
// Add the new value inside of the Set
uniqueSet.add(calendarIconId);
// Turn the set into an array
const array = Array.from(uniqueSet);
console.log(array);
}
handleClicked('autumn=2020-08-20');
handleClicked('autumn=2020-08-22');
handleClicked('autumn=2020-08-22');
handleClicked('autumn=2020-08-22');
handleClicked('autumn=2020-08-22');
handleClicked('autumn=2020-08-22');
handleClicked('spring=2020-04-20');
handleClicked('spring=2020-04-21');
handleClicked('spring=2020-04-21');
handleClicked('spring=2020-04-21');
handleClicked('spring=2020-04-21');
handleClicked('spring=2020-04-21');
EDIT : We only want to keep one value for each season :
// Declaration of an object. We are going to use the key/value system
const library = {};
function handleClicked(calendarIconId) {
// We "open" the value to extract the relevant informations
const [
key,
value,
] = calendarIconId.split('=');
// Add the new value inside of the object
library[key] = value;
// Turn the object into an array, we gotta rebuild the values
const array = Object.keys(library).map(x => `${x}=${library[x]}`);
console.log(array);
}
handleClicked('autumn=2020-08-20');
handleClicked('autumn=2020-08-22');
handleClicked('autumn=2020-08-22');
handleClicked('autumn=2020-08-22');
handleClicked('autumn=2020-08-22');
handleClicked('autumn=2020-08-22');
handleClicked('spring=2020-04-20');
handleClicked('spring=2020-04-21');
handleClicked('spring=2020-04-21');
handleClicked('spring=2020-04-21');
handleClicked('spring=2020-04-21');
handleClicked('spring=2020-04-21');
score:0
You can do
if(!Array.includes(id)) {
Array.push(id)
}
Source: stackoverflow.com
Related Query
- How can i avoid adding duplicates into array and only have one value at time for specific attribute?
- How can I map the complete json data into my code? Right now I can only do it for one entry at a time
- useState hook can only set one object at the time and return the other object of the same array to initial state
- How can you check if there is an existing value in the array and not add it another time if so?
- How can I have only one onChange for several inputs in reacthooks?
- How I can get check box value and button value in this stuation? Can any one have suggest
- Redux + React how to change only one element value in array full of objects? I have working app, just want to make sure I do it right
- How to use grid layout for array of items in one column when other columns have only one item?
- How can I refactor my React component to accept an array of errors and shows an error message if error.show = true for any one of those errors
- How to do a foreach for an array with values that have to match specific key and the same value from another array
- How to find if one of the items have completed property with value false from an array of objects using javascript and react?
- React/Mobx: How to pass single item from a mapped array into another component and retrieve only the item associated with the value clicked
- how can i focus only one button at a time and not several at the same time?
- How can I use Map function for an Array with values and another array as a value in Javascript
- How to create epic that emits another one and at the same time listens for its end only once and until it fails?
- How can i get array of object from this data for state and count in reactjs
- How to create array from state value and setstate after removing duplicates in reactjs
- How can I update a hook from one component and have it update another component as a result?
- How to get only one value in a variable of parent component, by selecting and switching to any of the option in reactjs?
- How can I render out the items in my array one at a time when returning them?
- how can i extract arrays from one array & convert them in object and put them in a new array?
- How to show only one active item at a time with react and material ui
- React.js: How to find duplicates for properties in an array of object and put a progressive number on that field
- How would I prevent duplicates in array of objects? And if there is a object with same property, it should sume its value
- How can I check only one check box at a time instead of all in a list of checkboxes in React
- How to use data stored in cache with SWR hooks, and how to make SWR fetch only one time
- How to consecutively add values from one array to another and store the value in a new array in javascript?
- how to store textarea new line value into array with react and send it to node js variable with post request
- How can I pass a value from the backend to a React frontend and use that value in the CSS for the frontend?
- React: how can I add states that are objects into a single array and access it?
More Query from same tag
- How to create a non modal dialog in material-ui
- How to implement a PureComponent in ES5
- Push items into empty State array of objects
- React onClick `this` binding
- Generic styled component Icon
- How to make an import shortcut/alias in create-react-app?
- How can I make React select component auto matically set to accomodate label width?
- localstorage values resetting after refresh and localstorage is not defined
- How to refresh another page in react JS
- Detect focus on children component with react
- React Semantic UI How to use "Link"
- React using graphQL apollo refetch will also update other components
- How could I know I am using ES6 in React?
- toggle menu in react only responds to first click
- REACT: Dynamical load multiple new HTML-Elements
- Pass a paramater to React through Express routing
- Do props that are functions belong in a useEffect dependencyArray?
- How to clear input fields after submit in react
- Cannot find module Error (React, TypeScript, Webpack)
- How to convert 2021-07-14T00:00:00.000Z into a standard date in YYYY-MM-DD format
- Modfying state in React using Onclick
- Countdown Timer function in React-Redux
- Javascript filter data based on multiple fields
- Route not functioning as expected on React
- How to destructure my api data in reactjs?
- Universal Reactjs with Nashorn and Tomcat
- Not able to call another action inside a action in react-redux
- Possibly 'undefined' error while compiling React code with onClick event
- React in HTML, blank page
- How to type a function in TypeScript