locked
traversing json objects RRS feed

  • Question

  • User1034446946 posted

    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?

    Saturday, September 7, 2019 1:38 PM

Answers

  • User-1460196090 posted

    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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, September 7, 2019 1:59 PM
  • User-474980206 posted
    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

    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.
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, September 7, 2019 4:29 PM

All replies

  • User-1460196090 posted

    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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, September 7, 2019 1:59 PM
  • User-474980206 posted
    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

    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.
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, September 7, 2019 4:29 PM
  • User1034446946 posted

    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.

    Saturday, September 7, 2019 8:33 PM
  • User-474980206 posted
    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#.

    Sunday, September 8, 2019 5:10 AM
  • User1034446946 posted

    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

    Sunday, September 8, 2019 2:20 PM
  • User-719153870 posted

    Hi EnenDaveyBoy,

    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?

    Best Regard,

    Yang Shen

    Monday, September 9, 2019 9:42 AM
  • User1034446946 posted

    looks like i was over complicating things, all good now and no where near as complicated.

    Monday, September 9, 2019 2:35 PM