0

I'd like to return the third node (hello3.com) of the key hello.com in javascript object.

 nodes = {
        "hello.com":
            { 
                id:"hello1.com",
                id2:"hello2.com",
                id3:"hello3.com"
            }
        }

I know that I can fetch all the key/values like this:

 newobject = nodes["hello.com"];

but how would I get the third. I'm aware that you can't count on the order in an object. If not, can I pull just the third by maybeb id3.

obreezy
  • 303
  • 1
  • 5
  • 18
  • You can try this ```newobject = nodes['hello.com']['id3']``` if this is the fixed keys that you want. – aavrug Dec 21 '16 at 05:05
  • as you have no array at all, there is no `nth element` – Jaromanda X Dec 21 '16 at 05:06
  • Object properties don't have an inherent order. If you want to guarantee order use an array. You *could* get a list of all property names (`Object.keys(nodes["hello.com"])`) and then sort that list and take the third one... – nnnnnn Dec 21 '16 at 05:13
  • @JaromandaX how would I make id1, id2 and id3 an array as the value of the key hello.com – obreezy Dec 21 '16 at 05:15
  • `[ {id1:"hello1.com"}, {id2: "hello2.com"}, {id3: "hello3.com"} ]` is one option to structure it as an array but keep the IDs too. – nnnnnn Dec 21 '16 at 05:17

5 Answers5

2

You answered your own question when you said that you can't count on the properties of an object to be in any certain order. If your properties are sequential in nature (your properties were counting up in your example), then I would suggest trying to use an Array.

nodes = {
    "hello.com": [
        "hello1.com",
        "hello2.com",
        "hello3.com"
    ]
};

In the above example, you would access the 3rd property with

nodes["hello.com"][2]

The double bracket notation is because "hello.com" is in quotes to allow a . in the name. If the key didn't require quotes, like helloCom as an example, you could use

nodes.helloCom[2]

Beyond this, if you name your keys sequentially, then you can impose an "order". It's not that any property is literally before or after another, but rather that you have informed yourself of what order YOU intend them to be.

Euroclydon37
  • 667
  • 7
  • 20
1

You can try this,

 nodes = {
   "hello.com": {
     id: "hello1.com",
     id2: "hello2.com",
     id3: "hello3.com"
   }
 }
 console.log(nodes["hello.com"]["id3"]);
ScanQR
  • 3,740
  • 1
  • 13
  • 30
  • 1
    This is great. This pulls the value of id3. So there's no way to pull the key name and value of the third element, except by knowing the name of that key id3? – obreezy Dec 21 '16 at 05:09
  • @obreezy yes always in `specific` cases you need to use your `key` to get value. Otherwise loop over and print everthing. If helpful please accept and upvote so that it will be helpful for others. Thanks. – ScanQR Dec 21 '16 at 05:15
0

BY INDEX :

About accessing by index, you can not achieve it directly. the closest you can get is array of keys but that also do not guarantee the order of keys returned. See this answer provided on other thread.

for (var i in nodes["hello.com"]) { console.log(i);//logs id,id2,id3 };

BY NODENAME:

nodes["hello.com"] returns object. You can use key to access the value by

1) using dot notation:

nodes["hello.com"].id3

2) or by bracket notation

nodes["hello.com"]["id3"]
Community
  • 1
  • 1
Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
0

Use:

nodes['hello.com'].id3 or nodes['hello.com']['id3']

Both are corrent way to get id3 data from given object

Sahadev
  • 1,368
  • 4
  • 18
  • 39
0

Try one of the following expressions

nodes["hello.com"]["id3"]

or

nodes["hello.com"].id3
VolAnd
  • 6,367
  • 3
  • 25
  • 43
yuanhehe
  • 26
  • 5