javascript 推送到阵列

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 推送到阵列相关的知识,希望对你有一定的参考价值。

// Creating an empty array for our favorite TV shows.
var favTVshows = [];
var tvShow;

// For-Loop that will repeat three times.
for (var i = 1; i < 4; i++) {

  // Each time it asks the user for their #1, #2, or #3 TV show.
  tvShow = prompt("What's your #" + i + " favorite TV show?");

  // It then takes the user's response and "pushes" (or adds) the variable to the end of the favTVshows array.
  favTVshows.push(tvShow);
}


// We then loop through the favTVshows array...
for (var j = 0; j < favTVshows.length; j++) {

  // ...and create alert messages listing out each show in our array.
  alert(favTVshows[j]);
}
var animals = ['pigs', 'goats', 'sheep'];

console.log(animals.push('cows'));
// expected output: 4

console.log(animals);
// expected output: Array ["pigs", "goats", "sheep", "cows"]

animals.push('chickens');

console.log(animals);
// expected output: Array ["pigs", "goats", "sheep", "cows", "chickens"]

以上是关于javascript 推送到阵列的主要内容,如果未能解决你的问题,请参考以下文章

为啥不需要的数据被推送到我的阵列中?

如何推送到嵌入式阵列?

无法使用猫鼬推送到阵列

MobX在推送到阵列时不更新React视图

打字稿推送到数组中的特定键

JavaScript 推送到数组