Basically, if you have a large list of objects in JavaScript and you have to iterate them, all until you find the match you compare to, it might introduce performance issues. However, if your objects list isn't that big, usually it's pretty much fine to iterate
and do comparison until you find the object based on its property value.
linq only works with collections, which are arrays in JavaScript. JavaScript has the many of the standard functional methods for navigating arrays, map, reduce, filter, find, etc. you should learn map, reduce and filter as these are linq’s select, group by,
and where methods. See list of methods
The global Map and Set object gives you the standard set operations of intersect and product (joins). All the array methods can be used when using map/set.
JavaScript objects are dictionaries in C#. In JavaScript when we use an object as a dictionary it’s called a mapped object (but it’s still just an object). The global Object has methods to return the the properties or keys as arrays. Of course key indexing
is supported.
so if i have want an property value out of the middle of a complex object, do i just loop through all the objects properties moving up the tree, then set the value to a const outside the loop and allow the loop to finish?
just seems a waste, when i get to the node i want, breaking out would reduce wasted traversing, but break causes an error.
most of my items are key:values so don't use arrays 90% of the time.
one of my problems i think is i am using c# terminology to google things, and i get the things which look like they should work but don't because of the array obj difference.
Why are you looping? If you have the key, just use it. You should explain what you are doing with a sample data. In general accessing complex objects is simpler in JavaScript than c#.
in the instance i am going at the moment, i can locate the list of objects i am interested in with a key, but then that is a list which i need to reference 3 properties to identify the object i need, hence the loop
in the instance i am going at the moment, i can locate the list of objects i am interested in with a key, but then that is a list which i need to reference 3 properties to identify the object i need, hence the loop
Is it possible for you to provide us a demo to tell what exactly you are trying to do or reproduce any problem or are you facing any error?
Participant
1778 Points
2678 Posts
traversing json objects
Sep 07, 2019 01:38 PM|EnenDaveyBoy|LINK
Hi
I am used to doing everythng in c# with lambda linq,and moving to javascript is muddling my mind.
i keep expecting there to be similar options in javascript, i keep getting linked to .find when i google, but thats just for arrays.
so in objs i just have to keep doing loops/iterating is until i get the element i want? and does that make loops/iterating a big thing in js?
Contributor
7042 Points
1772 Posts
Re: traversing json objects
Sep 07, 2019 01:59 PM|hajan|LINK
Hi there,
Basically, if you have a large list of objects in JavaScript and you have to iterate them, all until you find the match you compare to, it might introduce performance issues. However, if your objects list isn't that big, usually it's pretty much fine to iterate and do comparison until you find the object based on its property value.
Check through this blog post: https://medium.com/backticks-tildes/iterating-through-javascript-objects-5-techniques-and-performance-tests-42b4a222a92b which you may find useful with some nice tips about iterating objects and some performance tips.
Let me know if this has helped.
Thanks,
Hajan
Hajan
Dont forget to the answer which solved your problem!
All-Star
53574 Points
13316 Posts
Re: traversing json objects
Sep 07, 2019 04:29 PM|bruce (sqlwork.com)|LINK
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
The global Map and Set object gives you the standard set operations of intersect and product (joins). All the array methods can be used when using map/set.
JavaScript objects are dictionaries in C#. In JavaScript when we use an object as a dictionary it’s called a mapped object (but it’s still just an object). The global Object has methods to return the the properties or keys as arrays. Of course key indexing is supported.
Participant
1778 Points
2678 Posts
Re: traversing json objects
Sep 07, 2019 08:33 PM|EnenDaveyBoy|LINK
so if i have want an property value out of the middle of a complex object, do i just loop through all the objects properties moving up the tree, then set the value to a const outside the loop and allow the loop to finish?
just seems a waste, when i get to the node i want, breaking out would reduce wasted traversing, but break causes an error.
most of my items are key:values so don't use arrays 90% of the time.
one of my problems i think is i am using c# terminology to google things, and i get the things which look like they should work but don't because of the array obj difference.
All-Star
53574 Points
13316 Posts
Re: traversing json objects
Sep 08, 2019 05:10 AM|bruce (sqlwork.com)|LINK
Participant
1778 Points
2678 Posts
Re: traversing json objects
Sep 08, 2019 02:20 PM|EnenDaveyBoy|LINK
in the instance i am going at the moment, i can locate the list of objects i am interested in with a key, but then that is a list which i need to reference 3 properties to identify the object i need, hence the loop
Participant
1780 Points
580 Posts
Re: traversing json objects
Sep 09, 2019 09:42 AM|Yang Shen|LINK
Hi EnenDaveyBoy,
Is it possible for you to provide us a demo to tell what exactly you are trying to do or reproduce any problem or are you facing any error?
Best Regard,
Yang Shen
Participant
1778 Points
2678 Posts
Re: traversing json objects
Sep 09, 2019 02:35 PM|EnenDaveyBoy|LINK
looks like i was over complicating things, all good now and no where near as complicated.