Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Ben Bacarisse Newsgroups: comp.lang.javascript Subject: Re: output values from json array gets undefined error Date: Wed, 21 Jun 2017 22:34:09 +0100 Organization: A noiseless patient Spider Lines: 72 Message-ID: <871sqd9ga6.fsf@bsb.me.uk> References: <79e8a7f7-9d55-460e-87bc-9af64681db0a@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="87dc7b0a9209590bac5db6e295cf7a3f"; logging-data="7330"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+T6MJjp7yqOxsbmJF3GE4cVRusVV+oHy4=" Cancel-Lock: sha1:jMiVl6w5ats4Of4sLpf01f6c08w= sha1:7jX5Z2F1pmBEkCfjC9pQ3KAeyqY= X-BSB-Auth: 1.fc073e5cac2b7482c7d5.20170621223409BST.871sqd9ga6.fsf@bsb.me.uk Xref: csiph.com comp.lang.javascript:33556 JRough 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.