JavaScript- MDN教程的阵列新长度



我正在通过有关MDN数组的教程,我不明白代码行" newlength"如何返回7的值?我知道数组中有7个项目,但是在末尾没有" .lengength"时如何进行。


我的代码 -

var myData = 'Manchester,London,Liverpool,Birmingham,Leeds,Carlisle';
var myArray = myData.split(',');
var newLength = myArray.push('Bristol');
console.log(myArray);
console.log(newLength);

这是控制台内的返回结果 -

[ 'Manchester',
  'London',
  'Liverpool',
  'Birmingham',
  'Leeds',
  'Carlisle',
  'Bristol' ]
7

thx提供帮助!

array.prototype.push()返回您推入数组的新长度:https://developer.mozilla.org.org/nl/nl/docs/web/web/javascript/javascript/reperference/global_objects/global_objectss/global_objectss/global_objectss/array/push

这很简单...功能推动返回您的数组的新长度。

var newLength = myArray.push('Bristol'); //Returns the new length

https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/array/push/push

兄弟,按下内容后,推送方法返回数组的长度,这样就是您获得7的原因。首先按下元素,然后将其存储在这样的新array字段中。

var mydata ='曼彻斯特,伦敦,利物浦,伯明翰,利兹,卡莱尔';

var myarray = mydata.split(',');

myArray.push('bristol');var newlength = myArray;

console.log(myarray);

console.log(newLength);

最新更新