如何从对象中获取价值?

发布于 2024-11-09 07:54:56 字数 537 浏览 0 评论 0原文

我写了一个对象,它有 4 个键和值。如何使用 for 循环分别获取键和值?

我尝试了下面的代码,但没有运气。

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);
}

谁能帮助我吗?我在此页面中使用 jQuery,因此 jQuery 解决方案也可以。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

拧巴小姐 2024-11-16 07:54:56

在 jQuery 中,您可以使用 $.each 进行循环。

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

});

但是,您的循环并不遥远:

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

在此 for..in 循环中,x 是键名称。然后,您可以使用标准成员运算符在对象 timeObject 上访问它。请参阅 for..in 的 MDC 文档

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.

暮色兮凉城 2024-11-16 07:54:56

我认为你应该做这样的事情:

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]);
    }
}

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]);
    }
}
我ぃ本無心為│何有愛 2024-11-16 07:54:56

你几乎明白了。最后不需要额外的

工作代码

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]);
}

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]);
}
何以笙箫默 2024-11-16 07:54:56

如果它仍然是实际的 - Object.keys()< /a> 可能会帮助你。演示: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]]);
}

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]]);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文