Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How do you get an object in an array of objects by a property of one of the objects

    So, I have an array of objects:
    var mycars = [
    {id: 1, name: "Saab"},
    {id: 2, name:"Volvo"},
    {id:3, name:"BMW"}
    ];

    and I want to get the car by the specific id which I have set, not the index that's automatically given. So, if I wanted to get the Saab, I would say this:

    document.write(mycars[{id:1}]);

    but obviously, that doesn't work. So, what am I doing wrong here? Or am I just completely off base?

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Each element in the array is an object having an id and a name. To get the name corresponding to an id you need to search the array to find the object with that id and then return the corresponding name. The easiest way to do this would be to set up a field to load the name into and then do the search.

    Code:
    var name = '';
    mycars.forEach(function(a) {if (a.id === 1) name = a.name;});
    console.log(name);
    note that document.write is as obsolete as Netscape 4.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,350
    Thanks
    11
    Thanked 589 Times in 570 Posts
    my oQuery routine makes this as easy as using jQuery:


    Code:
    <script src="http://danml.com/js/oquery.js"></script>
    <script>
    
    r=[
      {id: 1, name: "Saab"},
      {id: 2, name:"Volvo"},
      {id:3, name:"BMW"}
    ];
    
    oQuery(r, "[id=2]"); // == {id: 2, name:"Volvo"}
    
    </script>
    you can also use felgall's simple hard-coded script, but i would suggest two minor alterations to it for speed and re-usability:

    Code:
    r=[
      {id: 1, name: "Saab"},
      {id: 2, name:"Volvo"},
      {id:3, name:"BMW"}
    ];
    
    var name = '';
    function byId(a) { return (a.id == this) && (name = a.name);}
    r.some(byId, 1);
    console.log(name);
    the some() method stops iteration once a match is found, eliminating un-needed work.
    Using "this" instead of a number literal means we can search for different IDs using the same function called from different points, making it easier to recycle as needed.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by rnd me View Post
    i would suggest two minor alterations to it for speed and re-usability:
    I agree. I had just thrown together an obvious way to code it to show the OP that you need to search for the id in the array. Your version is far more efficient and flexible.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •