Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #33556

Re: output values from json array gets undefined error

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.javascript
Subject Re: output values from json array gets undefined error
Date 2017-06-21 22:34 +0100
Organization A noiseless patient Spider
Message-ID <871sqd9ga6.fsf@bsb.me.uk> (permalink)
References <79e8a7f7-9d55-460e-87bc-9af64681db0a@googlegroups.com>

Show all headers | View raw


JRough <janis.rough@gmail.com> writes:

> I am getting undefined in console.log.  What is wrong?  thanks,

The console.log call should write [].  The result of the overall
function call is undefined but you should know that since you wrote it.

> var data =  [
>       {
>         "cityId": "574",
>         "cityAndState": "Allen, TX"
>       },
>       {
>         "cityId": "722",
>         "cityAndState": "Arlington, TX"
>       },
>       {
>         "cityId": "1736",
>         "cityAndState": "Carrollton, TX"
>       },
>       {
>         "cityId": "1737",
>         "cityAndState": "Dallas, TX"
>       },
>       {
>         "cityId": "1739",
>         "cityAndState": "Baltimore, MD"
>       },
>       {
>         "cityId": "1740",
>         "cityAndState": "Ann Arbor, MI"
>       },
>       {
>         "cityId": "1741",
>         "cityAndState": "Detroit, MI"
>       },
>       {
>         "cityId": "1742",
>         "cityAndState": "Royal Oak, MI"
>       },
>       {
>         "cityId": "1743",
>         "cityAndState": "Maddison Heights, MI"
>       }
>     ]
>   
>
> var iterateData = function(data){
> var ids = [];
>     if (data.hasOwnProperty(key)){

You don't want to use key here.  It gets set below, but you never get
there because this test is false.

>       for (var key in data){

You are probably better off just using a counted for here.

>         if( data[key].cityAndState  == "Carrollton, TX"  ||  data[key].cityAndState =="Allen, TX"){
>           ids.push(data[key].cityId);
>            console.log(ids);
>            }
>        }
>
>     }
>     console.log(ids);
> }
> iterateData(data);


-- 
Ben.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

output values from json array gets undefined error JRough <janis.rough@gmail.com> - 2017-06-21 14:18 -0700
  Re: output values from json array gets undefined error Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-21 22:34 +0100
    Re: output values from json array gets undefined error JRough <janis.rough@gmail.com> - 2017-06-21 15:54 -0700
      Re: output values from json array gets undefined error Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-22 00:45 +0100

csiph-web