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


Groups > comp.lang.javascript > #33555 > unrolled thread

output values from json array gets undefined error

Started byJRough <janis.rough@gmail.com>
First post2017-06-21 14:18 -0700
Last post2017-06-22 00:45 +0100
Articles 4 — 2 participants

Back to article view | Back to comp.lang.javascript


Contents

  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

#33555 — output values from json array gets undefined error

FromJRough <janis.rough@gmail.com>
Date2017-06-21 14:18 -0700
Subjectoutput values from json array gets undefined error
Message-ID<79e8a7f7-9d55-460e-87bc-9af64681db0a@googlegroups.com>
I am getting undefined in console.log.  What is wrong?  thanks,


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)){
      for (var key in data){
        if( data[key].cityAndState  == "Carrollton, TX"  ||  data[key].cityAndState =="Allen, TX"){
          ids.push(data[key].cityId);
           console.log(ids);
           }
       }

    }
    console.log(ids);
}
iterateData(data);

[toc] | [next] | [standalone]


#33556

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2017-06-21 22:34 +0100
Message-ID<871sqd9ga6.fsf@bsb.me.uk>
In reply to#33555
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.

[toc] | [prev] | [next] | [standalone]


#33558

FromJRough <janis.rough@gmail.com>
Date2017-06-21 15:54 -0700
Message-ID<adc8ddfb-b7c7-4aac-be1e-013d51a538af@googlegroups.com>
In reply to#33556
On Wednesday, June 21, 2017 at 2:34:14 PM UTC-7, Ben Bacarisse wrote:
> 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);
> 
> 
> -- 
> Be
Thanks, I did take out the first  for in  using a regular for loop.  It seems like it should work but it still says undefined.


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 = [];
    
      for (var i=0; i<=data.length;i++){
        var obj = data[i];
        for (var key in obj){
          if( obj[key].cityAndState  == "Carrollton, TX"  ||  obj.cityAndState =="Allen, TX"){
              ids.push(obj[key].cityAndState);
              console.log(ids);
            }
          }
         }
       }

iterateData(data);

[toc] | [prev] | [next] | [standalone]


#33560

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2017-06-22 00:45 +0100
Message-ID<87vano9a7u.fsf@bsb.me.uk>
In reply to#33558
JRough <janis.rough@gmail.com> writes:
<snip>
> Thanks, I did take out the first for in using a regular for loop.  It
> seems like it should work but it still says undefined.

I wonder how you got from what I said to the changes you ended up making
to the code.  I ask simply so that I will know what sort of thing I
might say in future to avoid whatever misunderstanding led to your mking
the code worse.

Maybe part of the problem is that you did not consider exactly where I
put the remarks.  You seem to quote everything and put a remark at the
end, whereas I put my remarks next to the part of the message to which
they applied.

<snip>
-- 
Ben.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web