JavaScript More elegant way to get filtered value?

I have a function that returns the filtered result based on string in the object (if it exist or not)

let foo = nodes.reduce((arr, cur) => {
    cur.classes.split(' ').filter((el) => {
        if (el === 'foo') arr.push(cur)
    })
    return arr; 
}, []);

So it's just return all objects in array that contains 'foo' in classes object This one, for example:

let nodes = [
  {node: 'h1', classes: 'foo'},
  {node: 'p', classes: 'bar'},
  {node: 'p', classes: 'baz xxx'},
  {node: 'h2', classes: 'bar baz foo'},
  {node: 'ul', classes: 'poop foo'}
]

But my guts tells that the function possible to write more easily and succinctly. Any ideas?

Answer:1

You could use just Array#filter.

let nodes = [{node: 'h1', classes: 'foo'}, {node: 'p', classes: 'bar'}, {node: 'p', classes: 'baz xxx'}, {node: 'h2', classes: 'bar baz foo'}, {node: 'ul', classes: 'poop foo'}],
    foo = nodes.filter(a => a.classes.split(' ').some(b => b === 'foo'));

console.log(foo);
Answer:2

I need to build a 2 way communication between a browser and an REST API service. One of the most complex thing I am facing is checking the API for updates every second. I have two types of ...

I need to build a 2 way communication between a browser and an REST API service. One of the most complex thing I am facing is checking the API for updates every second. I have two types of ...

  1. websocket communicate with each other
  2. websocket communication

So, I have this array of objects: var obj = [{ "Has the house been sold?": "N" }, { "Is the house on the market?": "Y" }, { "Duration of Sale": "2 weeks" }]; and I am trying to turn it ...

So, I have this array of objects: var obj = [{ "Has the house been sold?": "N" }, { "Is the house on the market?": "Y" }, { "Duration of Sale": "2 weeks" }]; and I am trying to turn it ...

  1. javascript return object from function
  2. javascript return object literal
  3. javascript return object values
  4. javascript return object from array
  5. javascript return object key
  6. javascript return object from map
  7. javascript return object type
  8. javascript return object object
  9. javascript return object name
  10. javascript return object undefined
  11. javascript return object from promise
  12. javascript return object without property
  13. javascript return object property
  14. javascript return object from foreach
  15. javascript return object as json
  16. javascript return object from callback
  17. javascript return object with new property
  18. javascript return object with highest value
  19. javascript return object from anonymous function
  20. javascript return object as string

Consider the following example snippet of my code. I have tried parseInt with no luck. Here, I want javascript to set a number in a cell PICweight then retrieve it later and place it in a different ...

Consider the following example snippet of my code. I have tried parseInt with no luck. Here, I want javascript to set a number in a cell PICweight then retrieve it later and place it in a different ...

  1. getelementbyid returns null
  2. getelementbyid returns undefined
  3. getelementbyid returns null react
  4. getelementbyid returns null javascript
  5. getelementbyid returns null angular
  6. getelementbyid() returns null for dynamic objects
  7. getelementbyid returns object htmlinputelement
  8. getelementbyid returns object
  9. getelementbyid returns string
  10. getelementbyid returns nothing
  11. getelementbyid returns multiple
  12. getelementbyid returns empty
  13. getelementbyid returns value
  14. getelementbyid returns reference
  15. document.getelementbyid returns null
  16. document.getelementbyid returns undefined
  17. document.getelementbyid returns null angular
  18. js getelementbyid returns null
  19. getelementbyid().value returns undefined
  20. document.getelementbyid returns null react

I'm trying to make a JavaScript search for a div within the same page. I'm currently using this code to display my search query: $('.form-search').on('submit',function(){return false;}); $('.form-...

I'm trying to make a JavaScript search for a div within the same page. I'm currently using this code to display my search query: $('.form-search').on('submit',function(){return false;}); $('.form-...