简体   繁体   English

如何在 Javascript 中推送和拉取多个值

[英]How to push and pull multiple values in Javascript

I'm pretty new to coding and am running into a roadblock.我对编码很陌生,并且遇到了障碍。 I'm working on a challenge that I just can't seem to figure out.我正在应对一个我似乎无法弄清楚的挑战。

function createArray() {
  var array = [];
   array.push = function(val){
     array[array.length] = val;
     return array;
   };
   array.pop = function(){
     return array[array.length - 1];
  };

 return array;
 };

var myArray = createArray();

When I run the test to complete the challenge, this code seems to push and pop a single value, but it doesn't seem to push and pop multiple values(which is one of the parameters for completing the challenge).当我运行测试以完成挑战时,此代码似乎推送和弹出单个值,但它似乎没有推送和弹出多个值(这是完成挑战的参数之一)。 Does anyone have any ideas?有没有人有任何想法? Possible solution?可能的解决方案? Any help would be amazing.任何帮助将是惊人的。

If you can use Array methods of course, but I thing you can, because you used array.length如果你当然可以使用 Array 方法,但我认为你可以,因为你使用了 array.length

array.pop = function () {
    return array.splice(array.length - 1, 1)[0];
}

array.push = function(value) {
    return array.concat(value)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM