如何使用无序索引迭代 JavaScript 中的对象

发布于 2024-12-06 05:17:43 字数 149 浏览 1 评论 0 原文

我正在创建一个播放列表系统,其中每首歌曲都有一个唯一的 ID。当您将歌曲添加到播放列表数组时,其索引将注册为其 ID。

通常,当循环 JavaScript 数组时,您会获取长度并通过索引进行计数。有没有办法循环遍历具有未使用索引的数组?这种做法出于某种原因是不好的吗?

I'm creating a playlist system where each song has a unique ID. When you add a song to the playlist array, its index is registered as its ID.

Normally when looping over a javascript array, you grab the length and count up through the index. Is there a way to loop through an array with unused indexes? Is that bad practice for any reason?

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

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

发布评论

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

评论(4

抱猫软卧 2024-12-13 05:17:43
var obj = {"123": "Lalala",
           "456": "dum dum"};
for(var i in obj){
    //i = ID
    // obj[i] = "song"
}

使用 for(var i in obj) 循环访问对象。请参阅上面的注释以了解此 for 语句的含义。

顺便说一句,您谈论的是对象,而不是数组:

var array = [7, 5, 9];

可以通过这种方式模拟数组,因此可以使用 for(var i=0; i用过的。数组函数(例如 pop)不能用于它们,除非您定义它:

var array_like = {0:7, 1:5, 2:9, length:3};
var obj = {"123": "Lalala",
           "456": "dum dum"};
for(var i in obj){
    //i = ID
    // obj[i] = "song"
}

Use for(var i in obj) to loop through an object. See the comments above to understand the meaning of this for-statement.

You're talking about Objects, not arrays, by the way:

var array = [7, 5, 9];

An array can be simulated in this way, so that a for(var i=0; i<array_like.length; i++) can be used. Array functions (such as pop) cannot be used on them, unless you define it:

var array_like = {0:7, 1:5, 2:9, length:3};
冷心人i 2024-12-13 05:17:43

您可以通过以下两种方式之一访问对象的属性。要么使用“.”例如:

var my prop = obj.property;

或者通过括号表示法:

var my prop = obj["property"];

现在,这并不能真正回答您的问题。所以,这里有一些方法:

利用 jQuery 的每个函数:

$.each(obj, function(key, value){
    //Do something with your key and value.
});

利用 Javascript 的函数:

for(var key in obj)
{
    var value = obj[key];
    //Do something with your key and value.
}

希望有帮助!

You can access the object's properties one of two ways. Either using a "." such as:

var my prop = obj.property;

Or via bracket notation:

var my prop = obj["property"];

Now, that doesn't really answer your question. So, here are a few ways:

Utilize jQuery's each function:

$.each(obj, function(key, value){
    //Do something with your key and value.
});

Utilize the Javascript for function:

for(var key in obj)
{
    var value = obj[key];
    //Do something with your key and value.
}

Hope that helps!

写下不归期 2024-12-13 05:17:43

您还可以仅对每个属性值进行迭代:

var obj = {"key1": "val1",
           "key2": "val2"};

for each(var value in obj) {
    ....
}

但在这种情况下,您无法访问属性名称。

You can also do just for each to iterate over properties values only:

var obj = {"key1": "val1",
           "key2": "val2"};

for each(var value in obj) {
    ....
}

But in this case, you are not able to access a property name.

神也荒唐 2024-12-13 05:17:43

假设您的数组采用以下形式:

array['id1'] = 'val1';
array['id2'] = 'val2';
array['id3'] = 'val3';

您可以使用以下方法迭代这些值:

for (var id in array) {  
  document.write(id + ' – ' + array[id]);
} 

Assuming your array is in the form:

array['id1'] = 'val1';
array['id2'] = 'val2';
array['id3'] = 'val3';

You can iterate over the values by using:

for (var id in array) {  
  document.write(id + ' – ' + array[id]);
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文