在推入数组后,数组的长度是多少,并将键值对指定为对象?

时间:2022-09-25 09:19:12

This is an example :

这是一个例子:

var myArray = []
myArray.push('A String')
console.log(myArray.length) // got: 1
myArray['aRandomKey']='An Other String'
console.log(myArray.length) // got: 1

So basically the second element is not added to the array as the length have not changed. But when i log the array :

所以基本上第二个元素没有被添加到数组中,因为长度没有改变。但是当我记录数组的时候:

console.log(myArray) // got: ["A String", aRandomKey: "An Other String"]

i see that myArray has 2 elements ... So what's going on ?

我看到myArray有两个元素…那么到底是怎么回事呢?

1 个解决方案

#1


0  

JSFIDDLE DEMO

JSFIDDLE演示

var myArray = []
myArray.push('A String')
console.log(myArray.length) // got: 1
myArray['aRandomKey']='An Other String'
console.log(myArray.length) // got: 1

Few more things

更多的事情

myArray[1] = "2nd string";
console.log(myArray.length);// you'll get 2

console.log(myArray.aRandomKey); // An Other String
console.log(myArray["aRandomKey"]); // An Other String
console.log(myArray) // ["A String", "2nd string", aRandomKey: "An Other String"]

By looking at the above statements, only if you use push() or assign using an integer key myArray[1], the value gets pushed into the array. If you add a non-number key instead of an integer, it still gets added to the array object as a property but doesn't get pushed as an item and can be accessed by the object notation like above.

通过查看上面的语句,只有在使用push()或使用整数键myArray[1]分配时,该值才会被推入数组中。如果您添加一个非数字键而不是整数,它仍然会被添加到数组对象中作为属性,但是不会被作为一个项被推,并且可以被上面的对象符号访问。

Note: Beware while adding an item to the array in this fashion myArray[1] = "2nd string"; For example, if you write like this myArray[1000] = "2nd string";console.log(myArray), the result looks like ["A String", 1000: "2nd string", aRandomKey: "An Other String"] and length will be 1001

注意:当在这个时尚myArray[1] = "2 string"中添加一个项到数组时要注意。例如,如果您像这样写myArray[1000] = "2 string";console.log(myArray),结果看起来就像[" string", 1000: "第二字符串",aRandomKey: "另一个字符串"],长度将是1001。

#1


0  

JSFIDDLE DEMO

JSFIDDLE演示

var myArray = []
myArray.push('A String')
console.log(myArray.length) // got: 1
myArray['aRandomKey']='An Other String'
console.log(myArray.length) // got: 1

Few more things

更多的事情

myArray[1] = "2nd string";
console.log(myArray.length);// you'll get 2

console.log(myArray.aRandomKey); // An Other String
console.log(myArray["aRandomKey"]); // An Other String
console.log(myArray) // ["A String", "2nd string", aRandomKey: "An Other String"]

By looking at the above statements, only if you use push() or assign using an integer key myArray[1], the value gets pushed into the array. If you add a non-number key instead of an integer, it still gets added to the array object as a property but doesn't get pushed as an item and can be accessed by the object notation like above.

通过查看上面的语句,只有在使用push()或使用整数键myArray[1]分配时,该值才会被推入数组中。如果您添加一个非数字键而不是整数,它仍然会被添加到数组对象中作为属性,但是不会被作为一个项被推,并且可以被上面的对象符号访问。

Note: Beware while adding an item to the array in this fashion myArray[1] = "2nd string"; For example, if you write like this myArray[1000] = "2nd string";console.log(myArray), the result looks like ["A String", 1000: "2nd string", aRandomKey: "An Other String"] and length will be 1001

注意:当在这个时尚myArray[1] = "2 string"中添加一个项到数组时要注意。例如,如果您像这样写myArray[1000] = "2 string";console.log(myArray),结果看起来就像[" string", 1000: "第二字符串",aRandomKey: "另一个字符串"],长度将是1001。