JavaScript array.filter paramter works only if wrapped around {} array_filter parameter,array_filter() expects parameter 2 to be a valid

I am new to JavaScript and Redux. I am trying to filter an array based on id in one of the reducers in Redux, it does not work if I only pass id to the callback function, but it works if the id is wrapped around {id}.

//Default store
const defaultState = {
  articles: [],
  filtercriteria: {}
};

//Reducer
const articlesReducer = (state = defaultState.articles, action) => {
  switch (action.type) {
    case "ADD":
      return [...state, action.article];
    case "REMOVE":
      //return state.filter(id => id !== action.id); //**DOES NOT WORK**
      return state.filter(({id}) => id !== action.id); // **WORKS**
    default:
      return state;
  }
  return state;
};

//Action Creator and Action
const remove = id => {
  return {
    type: "REMOVE",
    id
  };
};

//Action Dispatch
const addActionObject = store.dispatch(add());
store.dispatch(remove(addActionObject.article.id));
Answer:1

Well because you are using destructuring

The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.

If you didn't use destructuring you would use the function like so:

state.filter((item) => item.id !== action.id);

But you can use destructuring to assign the existing property "id" to a distinct variable

state.filter(({ id }) => id !== action.id);
Answer:2

If you want it to work without the brackets just do this:

return state.filter(obj => obj.id !== action.id);

The comments above from Pointy gave the rest of the answer. Happy coding

Answer:3

Pointy is completely right. There is a place for the object destructuring. The thing is you are filtering an array of objects (articles). Filter gets a predicate function which is the first argument is element of array.

To make a shorter version you can use object destructuring. Also, you could simply write like this ...filter(article => article.id !== action.id);

Here you can read a bit more about object destructuring. And here a documentation for array filter.

Answer:4

I have some form fields that are dynamically generated form the database. There are inputs, checkboxes, radio buttons, textarea's, and select's. All the fields are in a parent div with ID dynamic-form-...

I have some form fields that are dynamically generated form the database. There are inputs, checkboxes, radio buttons, textarea's, and select's. All the fields are in a parent div with ID dynamic-form-...

  1. check required fields before submit
  2. check required fields before submit angular
  3. check required fields javascript
  4. check required fields jquery
  5. check required field validation in javascript
  6. check required field validation in jquery
  7. check required fields js
  8. check required fields before submit html
  9. check required fields
  10. check required fields on click
  11. check required field in angularjs
  12. check required fields before submit react
  13. check required field in angular
  14. check required fields form
  15. check required field using javascript
  16. check required field validation in angular
  17. javascript check required fields before submit
  18. check all required fields jquery
  19. check if required fields are filled jquery
  20. check if required fields are filled

I've read that javascript was in part based on Scheme, a dialect of LISP. When I was reading about this and LISP, it struck me that the javascript function expression syntax seemed like a similar kind ...

I've read that javascript was in part based on Scheme, a dialect of LISP. When I was reading about this and LISP, it struck me that the javascript function expression syntax seemed like a similar kind ...

  1. javascript function expressions
  2. javascript function expression vs declaration
  3. javascript function expression vs statement
  4. javascript function expression hoisting
  5. javascript function expression example
  6. javascript function expression syntax
  7. javascript function expression parameters
  8. javascript function expression or declaration
  9. javascript function expression arrow
  10. javascript function expression arguments
  11. javascript function expression semicolon
  12. javascript function expression constructor
  13. javascript function expression vs
  14. javascript function expression call
  15. javascript function expression without var
  16. javascript function expression name
  17. javascript function expression closure
  18. javascript function expression prototype
  19. javascript function expression undefined
  20. javascript function expression return value

I am trying to make a Python 3 application to download weather data from my account at http://www.osanywhereweather.com. I have found JavaScript source code that does exactly this at https://github....

I am trying to make a Python 3 application to download weather data from my account at http://www.osanywhereweather.com. I have found JavaScript source code that does exactly this at https://github....

  1. javascript crypto hash
  2. javascript cryptographic hashing
  3. js crypto hash
  4. cryptographic hash js
  5. js crypto hashes

I am trying to create a Partial View that is a submission form for creating a a new ProductionGoal model. It uses ProductionLineViewModel to create that. My main question is how to pass that data ...

I am trying to create a Partial View that is a submission form for creating a a new ProductionGoal model. It uses ProductionLineViewModel to create that. My main question is how to pass that data ...

  1. create viewmodel from model
  2. create viewmodel from entity framework
  3. create viewmodel from model mvc
  4. create viewmodel from multiple models
  5. creating viewmodel in mvc
  6. create viewmodel in fragment
  7. create viewmodel in xaml
  8. create viewmodel in android
  9. create viewmodel in jquery
  10. create viewmodel in mvc 5
  11. create viewmodel in controller
  12. c# create viewmodel from model
  13. wpf create viewmodel from view