Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 142239

Javascript - Array length doesn't work if characters "{","}" or ":" are present

$
0
0

everyone! I have an array of items and i want to return a number of how many values match a specific State in US. For example,this is a working code: https://jsfiddle.net/fkj3bq5g/

JS:

const arr = ["Texas","Austin","TX","Texas","San Antonio","TX","California","Los Angeles","CA"];
const results = arr.filter(elem => elem === "Texas").length;
document.getElementById("demo").innerHTML = results;

html:

<span>Texas Cities:</span><span id="demo"></span>

I''m trying to match "Texas" and it should return "2",which it does. However,i encounter a problem as soon as i'm trying to put an array from of a .json file which have characters like { , } and : - i'm not getting any results. I'm not sure why,since i'm pretty much a noob anyway.

Here's an example how the array in the .json file looks like:

var state_city = [
{"state":"Texas","city":"Houston","code":"TX"},
{"state":"Texas","city":"Dallas","code":"TX"},
{"state":"Texas","city":"Austin","code":"TX"},
{"state":"California","city":"Los Angeles","code":"CA"},
{"state":"California","city":"Sacramento","code":"CA"},
{"state":"California","city":"Anaheim","code":"CA"},
]

How do i make this work?


Viewing all articles
Browse latest Browse all 142239

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>