4

I wrote an object, it has 4 keys and values. How can I get the keys and values separately using a for loop?

I tried the below code, but no luck.

var timeObject = {
    getNewYorkLocalTime: 'getTime.php?lat=40.7143528&lan=-74.0059731',
    getLondonLocalTime: 'getTime.php?lat=51.5001524&lan=-0.1262362',
    getChennaiLocalTime: 'getTime.php?lat=13.060422&lan=80.249583',
    getBangaloreLocalTime: 'getTime.php?lat=12.9715987&lan=77.5945627'
}

for (var x in timeObject) {
    alert(timeObject[x].value);
}

Can anyone help me? I'm using jQuery in this page, so a jQuery solution is ok too.

4 Answers 4

2

In jQuery, you can loop through with $.each.

$.each(timeObject, function(key, value) {

});

However, your loop isn't far off:

for (var x in timeObject) {
    alert('key: ' + x + ' value=' + timeObject[x]);
}

In this for..in loop, x is the key name. You can then access it on the object timeObject using the standard member operator. See the MDC documentation for for..in.

Sign up to request clarification or add additional context in comments.

Comments

1

I think you sholu do something like this:

var timeObject = {
    getNewYorkLocalTime: 'getTime.php?lat=40.7143528&lan=-74.0059731',
    getLondonLocalTime: 'getTime.php?lat=51.5001524&lan=-0.1262362',
    getChennaiLocalTime: 'getTime.php?lat=13.060422&lan=80.249583',
    getBangaloreLocalTime: 'getTime.php?lat=12.9715987&lan=77.5945627'
}

for (var x in timeObject) {
    //use this check to avoid messing up with prototype properties
    if (timeObject.hasOwnProperty(x)) {
        alert(timeObject[x]);
    }
}

3 Comments

as far as I know, prototype properties won't be applicable here since you're working with an object and not a native array.
@JohnP Unless you've defined Object.prototype.contains or somesuch.
@JohnP I think i'ts always better if you filter out any prototype chains if you don't need it.
0

You almost got it. You don't need the extra value at the end.

Working code

var timeObject = {
    getNewYorkLocalTime: 'getTime.php?lat=40.7143528&lan=-74.0059731',
    getLondonLocalTime: 'getTime.php?lat=51.5001524&lan=-0.1262362',
    getChennaiLocalTime: 'getTime.php?lat=13.060422&lan=80.249583',
    getBangaloreLocalTime: 'getTime.php?lat=12.9715987&lan=77.5945627'
}

for (var x in timeObject) {
    console.log(timeObject[x]);
}

Comments

0

If it's still actual - Object.keys() may help you. Demo: http://jsfiddle.net/SK4Eu/

var timeObject = {
    getNewYorkLocalTime: 'getTime.php?lat=40.7143528&lan=-74.0059731',
    getLondonLocalTime: 'getTime.php?lat=51.5001524&lan=-0.1262362',
    getChennaiLocalTime: 'getTime.php?lat=13.060422&lan=80.249583',
    getBangaloreLocalTime: 'getTime.php?lat=12.9715987&lan=77.5945627'
}

var keys = Object.keys(timeObject),
    keysLength = keys.length;

for (var i = 0; i < keysLength; i++) {
    alert(timeObject[keys[i]]);
}

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.