score:2
Accepted answer
You can do like this as below code
setAvailableUnits(data.filter((v)=> v.status === "AVAILABLE"));
setAssignedUnits(data.filter((v)=> v.status === "ASSIGNED"));
score:0
This will only work if there are just two statuses available: AVAILABLE
and ASSIGNED
. Modify the if statement if you want to include more statuses.
const units = [
{id: 1, status: "AVAILABLE"},
{id: 1, status: "ASSIGNED"},
];
const newAvailableUnits = [];
const newAssignedUnits = [];
units.forEach((unit) => {
if (unit.status === "AVAILABLE") newAvailableUnits.push(unit);
else newAssignedUnits.push(unit);
})
setAvailableUnits(newAvailableUnits);
setAssignedUnits(newAssignedUnits);
score:1
if units
is already known before rendering the component you can try this
const [availableUnits, setAvailableUnits] = useState(units.filter((v)=> v.status === "AVAILABLE"));
const [assignedUnits, setAssignedUnits] = useState(units..filter((v)=> v.status === "ASSIGNED"));
if not and it comes from fetching
for example
you can try
setAvailableUnits(data.filter((v)=> v.status === "AVAILABLE"));
setAssignedUnits(data.filter((v)=> v.status === "ASSIGNED"));
Source: stackoverflow.com
Related Query
- Filtering an array of objects into two new arrays and saving each to useState
- A lookup function to match the same IDs in two different arrays of objects and inserting key/value pairs into one of the array of objects
- How can I compare two arrays of objects and add new key to an objects in the second array
- Compare Two different arrays and update array of objects with useState in React
- comparing name property on objects in two separate arrays and creating a new filtered array in React
- Comparing two arrays of nested objects and return new array of objects if compared values are not the same in javascript
- Filtering two arrays based on another array values and also push the unavailable values into another array
- Filter array of objects into new arrays
- Comparing two arrays and filtering one array upon this in javascript and react
- Merge two arrays into one Array of objects React
- fetching data from the Dog API using axios and saving data into a new array
- Filtering an object based on key, then constructing new array of objects based on the keys and values
- Merge two arrays of different objects into one array of objects combined from the first two
- modify and insert new object in array of objects with useState hook
- How to concat string properties of an array in an array of arrays and create a new property in each array
- Find and push results in two objects being pushed into the array
- How do I take an array of objects from state and push a specific value of the objects into a new array
- How do I iterate through an Array of Objects and display each objects into a Chart?
- compare two arrays of Objects in JavaScript and if true save new items to MongoDB
- Matching similar (momentJs) dates between two arrays of (momentJs) dates into a new array
- How can I use Promise.all on an array of objects and then reassign them to the relevant key in a new object?
- Comparing two arrays and adding objects to the other one
- filtering an array of objects based on a contained arrays contents. Vanilla JS, lodash, some other react releated technology?
- mapping and filtering an array of objects with key
- Merging two arrays into one that has the same amount of objects
- In Javascript, replace forEach that involves filtering the same array of objects each time through loop with a left join
- Looping through two object arrays to push matching values to a new array doesn't work in React but does in JS Fiddle
- Javascript 2 arrays of objects , get a value from one array of objects and assign it to the other array of objects
- Turn array of objects into list of options having label and value
- Convert two arrays to one object, with same key for each array in Javascript
More Query from same tag
- Don't remount component but change values
- req.body is empty and I can't figure out why
- Assigned returned value from function chain into a variable in javascript
- Uncaught (in promise) SyntaxError: Unexpected token in React
- Why the execution of my code gets stuck as it encounters the function call?
- Google App Engine - Razzle isomorphic react app - serve issues with no client bundles or assets
- CsvDownloader leaves data empty
- React-select: how to close panel?
- passing state with link
- Redux-thunk action returns a function, which is not executed, so data is not fetched
- ReactJS - issue about Router
- What is the best way of defining optional propTypes in react?
- Why useEffect does not behave like what i expect?
- filter in react with multiple conditions
- Storybook: Failed to execute 'createElement' on svg files using @svgr/webpack
- Axios get function returns a promise in React
- How do I access a class variable of a mixin in a reactjs class?
- Type React component with ref? Typescript
- ReactJS: Render Child Component Only
- create simple player with HTMLAudioElement in reactjs
- Typeerror : failed to fetch while Uploading file in SharePoint using pnpjs
- how to link to arrays, so when one item is click will find and display its pair?
- React component not replaced
- How to import method which is default exported from object in React?
- React dynamic import doesn't load classes
- Setting state when Firebase authentication state changes in ReactJS
- Expected an assignment or function call and instead saw an expression on ternary
- Passing function to child component this.props.children method undefined
- How can I handle this API with its different ways of sorting data in my page?
- Alignment of grid items in Material-ui