Get the value of the only entry in a hash [duplicate] - javascript

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Access the first property of an object
I have a hash h decoded from JSON, and it contains only one entry. I don't care (and I don't need to know) about the key for this entry, but just want to get its corresponding value.
What is the fastest method?

It would be much, much nicer if you knew what the key was, but if not, this should work
var value;
for (var key in h){
value = h[key];
break;
}

Related

I want toarray in JavaScript? Where is it my fault? [duplicate]

This question already has answers here:
Object property name as number
(6 answers)
Closed 4 years ago.
I add This code to my page and I want to complete city when user tap a zipcode
and my alert dosen't show
var obj = {
"01400": "ABERGEMENT-CLÉMENCIAT",
"01640": "ABERGEMENT-DE-VAREY",
"01500": "AMBÉRIEU-EN-BUGEY",
"01330": "AMBÉRIEUX-EN-DOMBES",
"01300": "AMBLÉON",
"01500": "AMBRONAY",
"01500": "AMBUTRIX",
"01300": "ANDERT-ET-CONDON",
"01350": "ANGLEFORT",
"01100": "APREMONT",
"01110": "ARANC",
"01230": "ARANDAS",
"01100": "ARBENT",
"01300": "ARBIGNIEU",
"01190": "ARBIGNY"
};
var myVariable = obj .01400;
alert(myVariable);
Firstly, there is no key named 97433 in your object
Secondly, even if there was you cannot use property accesors with object keys which begin with a number. You need to use bracket notation.
Lastly, use console.log() for debugging as alert() coerces types and blocks the UI logic.
var myVariable = obj['97433'];
console.log(myVariable);

How to remove an array element from local storage? [duplicate]

This question already has answers here:
Remove array item from localstorage
(4 answers)
Closed 4 years ago.
I have an array of contacts in local storrage, and I need to remove, for example, the first element. How better to do? Is this expression correct?
localStorage.removeItem("allContacts"[0]);
localStorage contains string values. If you stringifyed an array and put it into localStorage, then you'll need to parse the array, delete the element you want, and then set the localStorage property again:
const allContacts = JSON.parse(localStorage.allContacts);
allContacts.shift();
localStorage.allContacts = JSON.stringify(allContacts);

How do I use a variable to find an element in an array? [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 6 years ago.
I'd like to take some user input and use it to find a certain object in an array. But when I try to do it with the code below, I get an undefined error. What am I doing wrong?
function findNextLevel() {
var currentLevel = parseFloat(document.getElementById("currentLevel").value);
var xpForLevel = trainerLevels.currentLevel;
document.getElementById("result01").innerHTML = xpForLevel;
}
I'm assuming that trainerLevels is an array and currentLevel is an index. If so, the way to access an element in an array at a certain index is to use brackets like so. Otherwise, could you provide more details in your question?
var xpForLevel = trainerLevels[currentLevel];
If this is the answer that you were looking for, then may I recommend that you use the parseInt rather than the parseFloat function for getting the index? And also, since it is user input, you may want to check that currentLevel is in the correct range as well.

JavaScript for(-each) loop [duplicate]

This question already has answers here:
How do I enumerate the properties of a JavaScript object? [duplicate]
(14 answers)
Closed 6 years ago.
I have a JSON-Array and want to loop though it. I know that there is no foreach-loop (like in PHP), but is there a possibility to access the field index of the array?
This is my json:
{
'username': 'Karl',
'email': 'xyz#abc.xx',
'user_id': 5,
...
}
I have some span elements on my page with a data-field attribute. E.g:
<span data-field="username">xyz</span>
The json-array is the return of an ajax-request. With it, I want to replace every element where the data-field="" matches the array-index.
Right know, I do it this way for each element in the json-array:
$("[data-field='username']").text(data.username);
$("[data-field='email']").text(data.email);
But with more and more elements this becomes a little bit ugly^^
Any other possibilties? I read something about transforming the json to an object, but I have no idea how to do.
The for each loop in javascript would look like below.
for(var key in Json){
$("[data-field='"+ key +"']").text(Json[key]);
}

How to take a value from a link using jquery [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 7 years ago.
I want to pass one value through ajax by taking the values from jQuery. But I am using link so I have problems taking the value. I tried the following,
<a id="addpa" class="ActionPopup" href="http://localhost:49951/admin/assignhome/Add?sPId=7">Add</a>
Jquery Code:
var spId = $("#addpa").prop("href"); // Here i am getting a whole Url
var thequerystring = getParameterByName("sPId");
The result is showing undefined. How to take the value of sPId? Give me ideas..
How to take the value of sPId?
Try using String.prototype.split() , Array.prototype.pop()
var spId = $("#addpa").prop("href").split(/=/).pop();

Categories

Resources