Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #32170 > unrolled thread
| Started by | jykl608@gmail.com |
|---|---|
| First post | 2017-01-11 22:51 -0800 |
| Last post | 2017-01-13 21:48 +0100 |
| Articles | 4 — 4 participants |
Back to article view | Back to comp.lang.javascript
traverse a complex object and find value jykl608@gmail.com - 2017-01-11 22:51 -0800
Re: traverse a complex object and find value "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-01-12 09:36 +0100
Re: traverse a complex object and find value Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-12 12:42 +0000
Re: traverse a complex object and find value Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-01-13 21:48 +0100
| From | jykl608@gmail.com |
|---|---|
| Date | 2017-01-11 22:51 -0800 |
| Subject | traverse a complex object and find value |
| Message-ID | <aea7f57c-56e8-4fe3-9804-68eacec8f7d8@googlegroups.com> |
Hello,
I've been playing around with open weather API (https://openweathermap.org/). After calling one of their API, i get the following object:
// START ==============================================
{
"coord": {
"lon": -123.12,
"lat": 49.25
},
"weather": [
{
"id": 800,
"main": "Clear",
"description": "clear sky",
"icon": "01d"
}
],
"base": "stations",
"main": {
"temp": 272.22,
"pressure": 1006,
"humidity": 67,
"temp_min": 266.15,
"temp_max": 275.15
},
"visibility": 16093,
"wind": {
"speed": 3.1,
"deg": 40
},
"clouds": {
"all": 1
},
"dt": 1484177700,
"sys": {
"type": 1,
"id": 1112,
"message": 0.2153,
"country": "CA",
"sunrise": 1484150629,
"sunset": 1484181512
},
"id": 6173331,
"name": "Vancouver",
"cod": 200
}
// END ================================================
I want to find the value of "temp" inside the "main" property, but I am not sure how.
I have written the following recursive function:
// START hasOwnPropertyDeep(obj, prop) =======================
function hasOwnPropertyDeep(obj, prop)
{
if(obj.hasOwnProperty(prop))
{
// CODE SHOULD END HERE
// BUT SOMEHOW IT GOES ALL THE WAY DOWN
// AND RETURNS FALSE
return obj[prop];
}
for(let p in obj)
{
let o = obj[p];
if( typeof(o) === "object" && o)
{
hasOwnPropertyDeep(o, prop);
}
}
return false;
}
// END hasOwnPropertyDeep(obj, prop) =========================
The above works only with the "shallow" properties. It returns the property values deeper in the object, but it doesnt stop at the return statement. Instead it cascades until the end and returns false.
Any suggestions would help
Thanks!!!
[toc] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2017-01-12 09:36 +0100 |
| Message-ID | <XnsA6FB61CB35FE2eejj99@194.109.6.166> |
| In reply to | #32170 |
jykl608@gmail.com wrote on 12 Jan 2017 in comp.lang.javascript:
> // START ==============================================
>
> {
> "coord": {
> "lon": -123.12,
> "lat": 49.25
> },
> "weather": [
> {
> "id": 800,
> "main": "Clear",
> "description": "clear sky",
> "icon": "01d"
> }
> ],
> "base": "stations",
> "main": {
> "temp": 272.22,
> "pressure": 1006,
> "humidity": 67,
> "temp_min": 266.15,
> "temp_max": 275.15
> },
> "visibility": 16093,
> "wind": {
> "speed": 3.1,
> "deg": 40
> },
> "clouds": {
> "all": 1
> },
> "dt": 1484177700,
> "sys": {
> "type": 1,
> "id": 1112,
> "message": 0.2153,
> "country": "CA",
> "sunrise": 1484150629,
> "sunset": 1484181512
> },
> "id": 6173331,
> "name": "Vancouver",
> "cod": 200
>}
>
> // END ================================================
>
> I want to find the value of "temp" inside the "main" property, but I am
> not sure how.
If the above is seen as a string "s":
var jsn = JSON.parse(s);
document.write(jsn.main.temp); // 272.22
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2017-01-12 12:42 +0000 |
| Message-ID | <877f60sb41.fsf@bsb.me.uk> |
| In reply to | #32170 |
jykl608@gmail.com writes:
> I've been playing around with open weather API
> (https://openweathermap.org/). After calling one of their API, i get
> the following object:
<snip>
> {
> "coord": {
> "lon": -123.12,
> "lat": 49.25
> },
> "weather": [
> {
> "id": 800,
> "main": "Clear",
> "description": "clear sky",
> "icon": "01d"
> }
> ],
<snip similar>
> "id": 6173331,
> "name": "Vancouver",
> "cod": 200
> }
>
> I want to find the value of "temp" inside the "main" property, but I
> am not sure how.
result.main.temp
> I have written the following recursive function:
>
> // START hasOwnPropertyDeep(obj, prop) =======================
>
> function hasOwnPropertyDeep(obj, prop)
> {
> if(obj.hasOwnProperty(prop))
> {
> // CODE SHOULD END HERE
> // BUT SOMEHOW IT GOES ALL THE WAY DOWN
> // AND RETURNS FALSE
> return obj[prop];
> }
>
> for(let p in obj)
> {
> let o = obj[p];
>
> if( typeof(o) === "object" && o)
> {
> hasOwnPropertyDeep(o, prop);
> }
> }
>
> return false;
> }
>
> // END hasOwnPropertyDeep(obj, prop) =========================
>
> The above works only with the "shallow" properties. It returns the
> property values deeper in the object, but it doesnt stop at the return
> statement. Instead it cascades until the end and returns false.
You have not get grasped the way recursion works so your description of
what's happening is some way off the mark. Any recursive call that does
find obj.hasOwnProperty(prop) to be true *will* "stop" at the return,
but that is not going to be the value of the top-level call.
For example, take the simpler call
hasOwnPropertyDeep({id: "x", main: { temp: 200 }}, "temp")
Here, obj.hasOwnProperty(prop) will be false so the code enters the for
loop. The first p ("id") gives a type of "string" but the second,
"main", has type "object". You call hasOwnPropertyDeep({temp: 200},
"temp") which will return 200 but you throw that value away and go round
the loop again. Now the properties are exhausted, the loop ends and the
top-level call returns false.
The fix is not hard (but I won't write it out unless you want me to
since you'll have more fun doing it yourself), but you should think
about the design of this function. Do you really need it? What happens
if the value of a property is false? What do you want to happen when
there are two or more possible results?
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2017-01-13 21:48 +0100 |
| Message-ID | <4346283.0VBMTVartN@PointedEars.de> |
| In reply to | #32170 |
Stefan Ram wrote: > jykl608@gmail.com writes: >> I want to find the value of "temp" inside the "main" >> property, but I am not sure how. > > My program »UFilter« shows how to recursively traverse an > object. > > It prints the contents of the global object. It was only > tested under a recent Firefox. IOW, not tested at all. > I think you can add your search into the loop starting with > »Object.getOwnPropertyNames( arg ).forEach« and then strip > the program from the parts you don't need (like the output > of the object). > > [a character desert, as usual] Priding yourself to be an educator, I have a hard time understanding why you would continue to write source code in a way that does not follow any widely accepted code style, and in general in a way that is very difficult to read. -- PointedEars FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix> <https://github.com/PointedEars> | <http://PointedEars.de/wsvn/> Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web