0
var allowed_ids = {
            332438809: "Bereznyak24",
            about: {
                address: "Gorky 84",
                average_sum: 50
            },
            489485425: "Bereznyak25",
            about: {
                address: "Sohnstr 41",
                average_sum: 100
            }
        };


var checked = childs[0].innerHTML.replace(/\D+/g, "");
console.log(allowed_ids.checked.about.address);

`

In checked variable I store either 332438809 or 489485425. Saying allowed_ids[checked] results in Bereznyak24 or Bereznyak 25. But how can I access address and average_sum values? allowed_ids.checked.about.address this piece of code is a wrong one and does not result in a proper responce. Thanks!

3
  • 5
    Your given object has duplicate keys about which overwrite the first one Commented Apr 3, 2019 at 16:37
  • Are you able to re-write the object so it will work as you need or are you stuck with that object? Commented Apr 3, 2019 at 16:38
  • @imvain2 can do so. What's the proper way to reorganize it? Commented Apr 3, 2019 at 16:39

4 Answers 4

1

allowed_ids needs to be an array, otherwise you are overwriting the about key each subsequent time you assign it.

var allowed_ids = [
{
  332438809: "Bereznyak24",
  about: {
    address: "Gorky 84",
    average_sum: 50
  }
},
{
  489485425: "Bereznyak25",
  about: {
    address: "Sohnstr 41",
    average_sum: 100
  }
}];


var checked = childs[0].innerHTML.replace(/\D+/g, "");
console.log(allowed_ids[checked].about.address);
Sign up to request clarification or add additional context in comments.

2 Comments

This is wrong OP Said "In checked variable I store either 332438809 or 489485425". And allowed_ids[checked] will return undefined. There is no reason to create array.
Then op should mark stackoverflow.com/a/55500297/6301806 as correct
1

Your object is not correct. You should use 332438809,489485425 as keys and store "Bereznyak24","Bereznyak25" as property of nested object. And use Bracket Notation to access dynamic property names

var allowed_ids = {
            332438809:{
              name:"Bereznyak24",
              address: "Gorky 84",
              average_sum: 50
            },
            489485425:{
                name:"Bereznyak25",
                address: "Sohnstr 41",
                average_sum: 100
            }
        };


var checked = '332438809';
console.log(allowed_ids[checked].address);
console.log(allowed_ids[checked].name);
console.log(allowed_ids[checked].average_sum);

Comments

1

Your object has duplicate keys. It would be better to store it like this:

var allowed_ids = {
            332438809:{
             name: "Bereznyak24",
             about: {
                address: "Gorky 84",
                average_sum: 50
              }
            },
            489485425: {
             name:  "Bereznyak25",
             about: {
                address: "Sohnstr 41",
                average_sum: 100
             }
           }
        };
// Assuming checked = 489485425
console.log(allowed_ids[checked].about.address)
// Result = Sohnstr 41

1 Comment

If this is what checked represents then this is more correct than my answer
0

Assuming 489485425 and 332438809 are your keys, you can do as follows.

var allowed_ids = {
            332438809: {
                name: "Bereznyak24",
                address: "Gorky 84",
                average_sum: 50
            },
            489485425: {
                name: "Bereznyak25",
                address: "Sohnstr 41",
                average_sum: 100
            }
        };
        
        
        console.log(allowed_ids[489485425].name);
        console.log(allowed_ids[489485425].address);
        console.log(allowed_ids[489485425].average_sum);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.