javascript 常用阵列方法

Posted

tags:

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

var arr = ['a', 'b', 'c'];

  // push() - Add a new element into an array
  arr.push('d');

  // pop() - remove the last element of an array and return that element
  arr.pop();

  // concat() - concatenates to an array. This will not affect the original array
  var newArr = ['e']
  arr.concat(newArr);

  // join() - This joins all the elements in an array to create a string and also will no modify the original Array
  arr.join();

  // reverse() - this reverses the elements in an . This will change the original array
  arr.reverse();

  // shift() - this will remove the first element in the array and return that element. This wiil change the original array
  arr.shift();

  // unshift('newArray') - add element to the begining of your array and return the length of the modified array
  arr.unshift('newArr');

  // slice() - this will select part of an array and return that new array you selected and the original array remain UNCHANGED
  arr.slice(startAtIndexOne, endMinusOne);

  // sort() - sort an array. this will change the original array
  arr.sort();

  // splice() - this is similar to slice(), but it will modify the original array instead of just returning new array.
  // array.splice(start[, deleteCount[, item1[, item2[, ...]]]])
  var months = ['Jan', 'March', 'April', 'June'];
  months.splice(1, 0, 'Feb');
  // inserts at 1st index position
  console.log(months);
  // expected output: Array ['Jan', 'Feb', 'March', 'April', 'June']

  months.splice(4, 1, 'May');
  // replaces 1 element at 4th index
  console.log(months);
  // expected output: Array ['Jan', 'Feb', 'March', 'April', 'May']
</script>

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

javascript 新阵列方法

常用PC服务器LSI阵列卡配置

阵列天线综合之切比雪夫低副瓣阵列设计Matlab

[opencv]常用阵列操作函数总结

华三阵列卡8g缓存

text Javascript ES6阵列图