何时在 JavaScript 中使用对象或数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4441139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 11:27:19  来源:igfitidea点击:

When to use an object or an array in javascript?

javascriptarraysobjectlanguage-features

提问by Derek Adair

I just found out that Arrays inherit directly from Object in javascript. I'm finding the difference between an array and an object is fairly minuscule.

我刚刚发现数组直接从 JavaScript 中的 Object 继承。我发现数组和对象之间的区别非常小。

How do i know when to use one over the other?

我怎么知道什么时候用一个而不是另一个?

回答by Alex

When you need to depend on the orderof the elements in the collection, use Arrays, when order is not important, use objects. Order is not guaranteed in objects, but they provide for fast key-value pair lookups.

当需要依赖集合中元素的顺序时,使用数组,当顺序不重要时,使用对象。在对象中不保证顺序,但它们提供快速键值对查找。

回答by Jonathon Bolster

I'd use an Array []when I'm dealing with a list of objects the same type* (like a list of strings) that I want the order of and an Object {}when I'm dealing with something that I want properties of (and the properties are of different types, usually).

Array []当我处理我想要顺序的相同类型的对象列表(如字符串列表)时,我会使用 ,当我处理Object {}我想要的属性(和属性通常是不同类型的)。

For example:

例如:

var numberNames = ["one","two","three","four"];

var computer = {
     motherBoard : "mother", 
     psu : psu_object(),
     ram : { 
             ram1 : GB,
             ram2 : TwoGB
     }
};

* This is just my preference. Arrays can contain elements of multiple different types.

* 这只是我的偏好。数组可以包含多种不同类型的元素。

回答by Felix Kling

They are just different data structures, they serve different purposes. ArrayextendsObjectand provides properties like lengthand methods like push()and pop().

它们只是不同的数据结构,它们用于不同的目的。Array延伸Object并提供属性喜欢length和方法喜欢push()pop()

Think of an object as a hash table and an array as a list.

将对象视为哈希表,将数组视为列表。

E.g. you can use arrays as queue or as a stack which would not be possible with objects.

例如,您可以将数组用作队列或堆栈,而这在对象中是不可能的。

On the other side if you want to store data and want to access a specific datum directly, you would use an object.

另一方面,如果您想存储数据并想直接访问特定数据,则可以使用对象。

In the end it boils down to the question which data structure is the right one for the job. Maybe neither of them is and you would need a tree instead (which can be implemented via objects).

最后归结为哪种数据结构适合这项工作的问题。也许它们都不是,而您需要一棵树(可以通过对象实现)。

回答by Josiah Ruddell

Objectsare a good candidate if you have a unique key.

Objects如果你有一个唯一的键,是一个很好的候选人。

Example Arrayvs. Object:

示例ArrayObject

var item = { id: '123', prop1: 456 };

var obj = {}, arr = [];

// add to object
obj[item.id] = item;

// add to array
arr.push(item);

// get from object
var got = obj[item.id];

// get from array
var got;
for(var i = 0; i < arr.length; i++)
    if(arr[i].id == item.id){
        got = arr[i];
        break;
    }

As you can see finding an object is much more expensive with an array(loop). Using an objectyou will not have access to all the arrayhelper methods (sort/push/etc) but you can still iterate an objects' properties using a for...inloop.

如您所见,使用array(循环)查找对象的成本要高得多。使用 aobject您将无法访问所有array辅助方法(sort/push/etc),但您仍然可以使用for...in循环来迭代对象的属性。

回答by Matt Ball

Objects keys are strings; array keys are integers. JavaScript objects are maps (String -> anything) and arrays are lists (ordered collections ofanything).

对象键是字符串;数组键是整数。JavaScript 对象是映射 ( String -> anything),数组是列表( 的有序集合anything)。

Does that help?

这有帮助吗?

回答by BoltClock

You often use arrays when you have a series of related items that you want ordered and indexed.

当您有一系列要排序和索引的相关项目时,通常会使用数组。