I would like to access an object property based on the first value in it's array.
var LANGS = {
"C#": [10, "text/x-csharp", "code"],
"C/C++": [7, "text/x-c++src", "code"]
};
So I want to be able to access "C#"
by the 10
in its array, how would I do this?.
You can use for-in
to loop through the object's property names, and then get the value of the property using the name in order to check its first element:
var LANGS = {
"C#": [10, "text/x-csharp", "code"],
"C/C++": [7, "text/x-c++src", "code"]
};
function getEl(number) {
for(var el in LANGS) {
if(LANGS[el][0] == number) {
return el;
}
}
}
alert(getEl(10));
var LANGS = { "C#": ["text/x-csharp", "code",10], "C/C++": [7, "text/x-c++src", "code"] }; function getEl(number) { for(var el in LANGS) { debugger var array = []; array = LANGS[el]; if(array.indexOf(number) > -1) { return el; } } } alert(getEl(10));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.