A
A
Aidar2017-04-27 13:13:41
Angular
Aidar, 2017-04-27 13:13:41

How to select from json by first letter of name in Angularjs?

Hello! How can you select in Angularjs by the first letter in an array? For example, there is this json file:

[
  {
    "code": "AD",
    "name": "Andorra",
    "population": "84000"
  },
  {
    "code": "AF",
    "name": "Afghanistan",
    "population": "29121286"
  }, 
  {
    "code": "BA",
    "name": "Bosnia and Herzegovina",
    "population": "4590000"
  },
  {
    "code": "BB",
    "name": "Barbados",
    "population": "285653"
  },    
]

I need to display only what starts with "A" in the name "code". Those. the result should be like this:
[
  {
    "code": "AD",
    "name": "Andorra",
    "population": "84000"
  },
  {
    "code": "AF",
    "name": "Afghanistan",
    "population": "29121286"
  },  
]

I read about custom filters for Angular and found an example, but it turned out to display by the full name.
jsfiddle.net/varnxmv1 is an example.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Fedwar, 2017-04-27
@Fedwar

arr.filter((city)=> {
return city.code.charAt().toLowerCase() === 'a';
})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question