'Why does the filter function return an empty array? [closed]
Why my filter function returns empty in javascript
const result = [{"name": "josh", ...},{"name": "mike", ...},{"name": "john", ...},{"name": "simmon", ...}]
const data = 'j'
const title = result.filter(x => x.name=== data )
It should return this
const result = [{"name": "josh", ...},{"name": "john", ...}]
Solution 1:[1]
You are checking if name is equal to j and therefore the filter is working as expected.
Try to use startsWith
const result = [{"name": "josh", ...},{"name": "mike", ...},{"name": "john", ...},{"name": "simmon", ...}]
const data = 'j'
const title = result.filter(x => x.name.startsWith(data) )
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | R4ncid |
