score:1
you are on a right way, but you are not using array.filter
properly.
and also the provided array of object is not also in a good format. object should be in a key:value
pairs.
state.products = [
{ id: 1, name: "bottle" },
{ id: 2, name: "umbrella" },
{ id: 3, name: "shoe" }
]
const getallarrayexcept = (id) => {
// this will return all the array except provided id
return state.products.filter((s) => s.id !== id)
}
const getonlyarray = (id) => {
// this will return only item which match the provided id
return state.products.filter((s) => s.id === id)
}
console.log(getallarrayexcept(1))
/*
output: [{ id = 2, name = umbrella }, { id = 3, name = shoe }]
*/
console.log(getonlyarray(1))
/*
output: [{ id = 1, name = bottle }]
*/
here is the working snippet:
const products = [
{ id: 1, name: "bottle" },
{ id: 2, name: "umbrella" },
{ id: 3, name: "shoe" }
]
const getallarrayexcept = (id) => {
// this will return all the array except provided id
return products.filter((s) => s.id !== id)
}
const getonlyarray = (id) => {
// this will return only item which match the provided id
return products.filter((s) => s.id === id)
}
console.log("all array except: ", getallarrayexcept(1))
console.log("only provided item in array: ", getonlyarray(1))
Source: stackoverflow.com
Related Query
- How can I filter through an array of objects based on a key in a nested array of objects?
- How to filter an array of objects based on ID?
- How do I filter the data based on search input from array of objects in React, ES6
- How to filter array of nested objects with unknown depth based on given search term
- How to filter and highlight text in the Dynamic nested array of JSON objects based on text match incase sensitive
- How to filter Dynamic nested array of JSON objects based on text match with both upper and lower case
- How to filter out array within array of objects based on date range?
- How to filter an array of objects based on another array of objects?
- ReactJS/ React-Router: How to filter nested array and objects based on url path parameters?
- How to filter an array of objects in react based on a specific dynamic property?
- How to filter an array based on an array of objects inside the array
- How to sort array of objects based on a boolean property?
- Filter Array of Objects based on input field in React
- ReactJS: How do I sort an array of objects based on value of props?
- How to filter an array of objects by their first letter in react/javascript
- react: how to filter an array of objects in props
- Filter array of objects based on the input passed: Javascript
- How to have an array of objects based routing in react?
- How to create an array filter based on values of another array - React
- How to filter or search an array of objects in React?
- How to filter an array based on another array?
- How to filter and modify an array of objects with a single line of code?
- How can I filter an array based on a category in react?
- How to filter value based on the nested array value?
- How can I filter from an Array that i created from another array objects in React?
- How to get value based on key from an array of objects javascript
- how to filter based on key from array using javascript?
- How to sort my array of objects based on firebase Timestamp
- How to Filter multi-select values in nested array of objects
- How do I map over an array of objects in React and then conditionally render a component based on a previous value?
More Query from same tag
- How do I render nth columns on every row using React JS?
- Can we pass setState as props from one component to other and change parent state from child component in React?
- React | Npm package - How can I export 2 components for use as an npm package
- Simple Redux app, only with Counter returns undefined when I use combineReducers
- How do I create a new object from states in React with typescript?
- How to combine column and staked bar highchart
- How do I toggle multiple displays separately in React
- nodemon app crashed after submitting form data
- Render Array of JSX.Element OR HTMLElement React Typescript
- How can I change npm version while using create-react-app
- Template doesn't react to the change of variable React
- Concatenate variable and sting in graphql query
- Make post request in redux-saga: problem with body of request
- How to add required rule to antd input with initial value as undefined?
- React-Firebase Authentication
- On input focus change parent class in React
- Let user control line breaks through input in react
- Clear parent state from child
- What would be a good diagram for designing React.js UIs before writing any code?
- How can I fix this Unit Test?
- What is the best option to use redux actions when using redux hooks?
- Dynamic loading of react components inside state
- Kendo React Grid width is 0
- I want to send the state from a component to another component to render it, how to do it without Redux?
- Navigate/select ag-grid table row with arrow keys
- Convert a class component into a function component
- Accessing cookies set at django backend server from the React client server
- React: call setState from another component
- react: helping to understand specific currying use case
- Express/React with CORS - Setting HTTP-Only Secure Cookie for React SPA