JavaScript Filter on array of objects, and foreach over second array for conditions filter array objects javascript,filter array objects angular 6,filter

I've hit a snag after a few hours -- wondered if a fresh minded developer can just review the below, the code is simplied to show the problem.

I am filtering a property value on an array of objects, and cross referencing that property with an array which has matching keys, and boolean values to control if it should be factored into the filter.

However my result is returning all 3 objects, despite the console.log seeming to evaluate correctly. Any ideas?

Many thanks...

var data = [{
    "id": 1,
    "status": "new",
  },
  {
    "id": 2,
    "status": "rejected",
  },
  {
    "id": 3,
    "status": "changed",
  }
];

var filter = {
  "new": true,
  "rejected": false,
  "changed": true
}

var result = data.filter(function(item) {
  var arr = [];

  Object.keys(filter).forEach(function(key) {
    if (item.status === key && filter[key] === true) {

      console.log('---')
      console.log('item.status', item.status)
      console.log('key', key)
      console.log('filter[key]', filter[key])
      console.log('---')

      arr.push(item);
    }
  });

  return arr;
});
Answer:1

You're making this a bit more complicated than you need to. The function passed to filter() should return a boolean — you're returning an array.

You can simply filter with the lookup on the filter array, which will either return false or undefined, in which case you filter it out, or true, in which case you keep it..

var data = [{
    "id": 1,
    "status": "new",
  },
  {
    "id": 2,
    "status": "rejected",
  },
  {
    "id": 3,
    "status": "changed",
  }
];

var filter = {
  "new": true,
  "rejected": false,
  "changed": true
}

var result = data.filter(item => filter[item.status])

console.log(result)
Answer:2

You filter by returning something truthy or falsey directly, not by returning anything more specific (like an object). You can simplify your code to a one-liner:

var data=[{"id":1,"status":"new",},{"id":2,"status":"rejected",},{"id":3,"status":"changed",}]
var filter = {
  "new": true,
  "rejected": false,
  "changed": true
}

var result = data.filter((item) => filter[item.status])
console.log(result);
Answer:3

I am new to Redux and i've been having a hard time rendering changes made to the store. I've been using Redux-DevTools to explore state changes and here is my problem. I have a sidebar which has ...

I am new to Redux and i've been having a hard time rendering changes made to the store. I've been using Redux-DevTools to explore state changes and here is my problem. I have a sidebar which has ...

  1. react redux component not updating
  2. react redux component not updating on state change
  3. react redux component lifecycle
  4. react redux component state
  5. react redux component example
  6. react redux component local state
  7. react redux component did mount
  8. react redux component communication
  9. react redux component dispatch
  10. react redux component not re rendering
  11. react redux component testing
  12. react redux component rendering twice
  13. react redux component
  14. react redux component will receive props
  15. react redux component container
  16. react redux component does not update
  17. react redux component did update
  18. react redux component vs container
  19. react redux component props
  20. react redux component library

I want to show pdf ajax response in browser.Problem is I can not directly open the url as pdf is created dynamically how can I show it. $.ajax({ url: url, type:...

I want to show pdf ajax response in browser.Problem is I can not directly open the url as pdf is created dynamically how can I show it. $.ajax({ url: url, type:...

By what logic js works (!+[]+[]+![]).length returns 9? How is that possible? As I know js has dynamic types cast but very hard to understand whats going on here console.log((!+[]+[]+![]).length);

By what logic js works (!+[]+[]+![]).length returns 9? How is that possible? As I know js has dynamic types cast but very hard to understand whats going on here console.log((!+[]+[]+![]).length);

  1. console log length of array
  2. console.log length
  3. console.log length limit
  4. console log length of string
  5. console.log max length
  6. intellij console log length
  7. javascript console.log length
  8. console.log('hi '.length)
  9. console.log maximum length
  10. eclipse console log length
  11. console.log array length 0
  12. console.log(data.length)
  13. console.log(array.length)
  14. javascript console.log max length
  15. javascript console log array length
  16. ie console log length
  17. console.log(foot.length)
  18. console.log($('ol li.even').length)

I'm trying to use the built in setValue() funtion in CRM 2015 to change a field back to null if a confirm() command comes back as false. The field that triggers the confirm() is also the field I am ...

I'm trying to use the built in setValue() funtion in CRM 2015 to change a field back to null if a confirm() command comes back as false. The field that triggers the confirm() is also the field I am ...