score:1
to use a dynamic field name, you have to use square brackets. so you would use:
cocktaillist.filter((cocktail) => cocktail[field] === value)
the issue you are going to run into is the nested key / value pairs under type
as you can't use something like type.value
with that notation.
score:0
you can do that with cocktail[field]
:
const drinktypehandler = (field, value) => {
const selectedtype = cocktaillist.filter((cocktail) => cocktail[field] === value);
console.log(selectedtype);
}
score:0
in order to access object property using a variable, you need to use dot notation
const drinktypehandler = (field, value) => {
const selectedtype = cocktaillist.filter((cocktail) => cocktail[field] === value);
console.log(selectedtype);
}
you can read more about bracket and dot notation here
Source: stackoverflow.com
Related Query
- Filter array based on dynamic objects and values in JS
- 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
- React- Filter array based on search and dropdown values
- Looping over an array of objects and showing values based on static keys defined javascript
- Javascript filter Array of objects with multiple conditions and values
- Filtering an object based on key, then constructing new array of objects based on the keys and values
- Map over array of objects and filter based on first name
- react js filter through an array of objects and compare values within to get the closest value to a variable
- Filter array of objects based on values in another array
- Trying to populate array with string values based on filter selections, and filter records by comparing the array to string value of record attribute
- 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?
- is there a way to filter an array of objects based on multiple checkbox values on checked change
- Filter Array of Objects based on input field in React
- Trying to filter array of object based on returned values
- Loop over an array of objects and format values
- Filter array of objects matching values from second array
- Filter array of objects based on the input passed: Javascript
- How can I filter through an array of objects based on a key in a nested array of objects?
- filter array of objects not returning filtered values
- Filter state array of data based on a unique key/name and push to a new Array
- How to create an array filter based on values of another array - React
- Filter the array of objects with optional values
- How to filter and modify an array of objects with a single line of code?
- Merge properties of objects within a array together using values and remove duplicate
- How to check the values of an array and delete some properties based on the condition?
- How to filter an array and add values to a state
- Sum total and make an array which count by same values from array of objects
- I'm trying to filter an array of Objects simultaneosly by number and by string value
More Query from same tag
- React JS with Redux does not appear to re-render
- "Object is possibly undefined" in for observable field
- Why is my React fetch weather API so buggy/temperamental?
- How to preserve the state for a "just hidden" component
- React Router: Show progress while loading
- REACTJS Add a condition to .map to check if the element is contained in an array
- React component flickers on first change in local state
- Is there any way to provide a shipping address to @paypal/react-paypal-js createOrder prop
- React.js, correct way to iterate inside DOM
- Is there a way to fix a component on first render to be transparent in background in React JS?
- Passing Function Down [React]
- Use context to share content after socket connection in React
- How do I call a function on button click to listen for another mouse click in React Hooks?
- Am i right that destructing object from object doesn't create new object
- How could I make the accordion close?
- Gradient in color of range column chart in ApexCharts
- How to access component function inside getDerivedStateFromProps
- React/Firebase -> DocumentReference.set() called with invalid data. Unsupported field value: undefined
- Why is OnChange not working when used in Formik?
- Material-ui combobox not showing the required data
- React-Virtualized table to grow up to a maximal height
- Purge-css is removing all css stylings instead of just the unused ones
- Can't figure out why onClick is not updating state
- How to deploy react app to godaddy server?
- Validating a non-required number field with Yup
- How to return different types based on parameters in a React.useCallback
- I am trying to dispatch a action but getting this error: " Unhandled Rejection (TypeError): Cannot read property 'type' of undefined"
- How to fix map is not function even there is already set data in the useState using React Hook
- React event-handler shows the same index value
- Why is my button triggering without click in RxJs fromEvent?