I have a 2d array called MyUsernames.
If I write console.log(MyUsernames) I see this:
0: {id: "5", name: "quirky.subdued"}
1: {id: "6", name: "phyllida.skeg"}
2: {id: "7", name: "duff.anarchist"}
3: {id: "9", name: "relashio.articulate"}
I am trying to access the name part of an element in the array.
I output the records in MyUsernames via this loop:
var hst = document.getElementById("usernames");
for (var i = 0; i < MyUsernames.length; i++) {
var un1 = MyUsernames[i].name;
hst.innerHTML += "<li>" +"<a id="+MyUsernames[i].id + " href='#content' onclick='deleteById(this)'>" + un1 + "</a></li>";
}
This is the deleteById bit of code:
var deleteById = function ( self ){
this_id = self.id;
this_word = MyUsernames[this_id].name;
}
My problem is that I am sending e.g. an ID value of 9 to extract the Name relashio.articulate.
However, I am trying to access that by this bit of code:
this_word = MyUsernames[this_id].name;
That doesn't work, because as per the console output, the bit I want in my example would be access via:
MyUsernames[3].name;
And not:
MyUsernames[9].name;
The problem is I can't send an ID value of 3 because I only have the ID value of the record from MyUsernames which is 9.
Is there a way I can access the "name" part of the array, by its corresponding ID?
users['9'].name;I hope you find posted answer useful, Good luck! [stackoverflow.com/a/64400342/14435535]