何时在 JavaScript 中使用对象或数组? [英] When to use an object or an array in javascript?

查看:26
本文介绍了何时在 JavaScript 中使用对象或数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

推荐答案

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

When you need to depend on the order of 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.

这篇关于何时在 JavaScript 中使用对象或数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆