Javascript数据类型 - Array(上)

2017-12-31  本文已影响0人  threetowns
ja-array-operation

一、 声明数组

There are two ways to declare an Array in javascript:

var foo = [];
var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
var foo = new Array();
var days = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');

二、属性(Properties)

length — the length property of an array is the largest positive integer key plus one.

三、方法(Methods)

There are many methods associated with arrays in javascript, some methods modify the existing array(Mutator Methods), some are used to access values or return some required representation and do not change the existing array(Accessor Methods), some for looping over array items(Iterator Methods) and so on. Lets explore few of most commonly used these methods and categories on the bases of there nature.

1. Mutator Methods

var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'];
days.push('Saturday');  //7
console.log(days);      // ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'];
days.pop(); //Friday
console.log(days); //["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"]
var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'];
days.shift();      // Sunday
console.log(days); // [ "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'];
days.unshift('Saturday'); // 7
console.log(days);        // [ "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
var numbers = [3,45,5,7,32,56,87,11];
numbers.sort(); //[11, 3, 32, 45, 5, 56, 7, 87]
console.log(numbers);  //[11, 3, 32, 45, 5, 56, 7, 87]
var numbers = [3,45,5,7,32,56,87,11];
numbers.reverse(); //[11, 87, 56, 32, 7, 5, 45, 3]
// Syntax - 
// array.splice(start, deleteCount, item1, item2, ...)

var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'];
days.splice(2,1);   // ["Tuesday"]
console.log(days);  // ["Sunday", "Monday", "Wednesday", "Thursday", "Friday"]
days.splice(2,0,'Tuesday');
console.log(days)   // ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]

2. Accessor Methods —

var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'];
days.slice(2,4); //["Tuesday", "Wednesday"]
days.slice(3); //["Wednesday", "Thursday", "Friday"]
var week_days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'];
var weekend_days = ['Saturday', 'Sunday'];
week_days.concat(weekend_days); //["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'];
days.join('-'); //Sunday-Monday-Tuesday-Wednesday-Thursday-Friday
var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'];
days.includes('Sunday'); //true
days.includes('sunday'); //false
var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'];
days.indexOf('Tuesday');    // 2
days.indexOf('Tuesday', 4); // -1
days.indexOf('March');      // -1
var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'];
days.lastIndexOf('Tuesday');    // 2
days.lastIndexOf('Tuesday', 4); // 2
days.lastIndexOf('March');      // -1

3.Iteration methods —

var random_numbers = [2,4,5,6,7,9,34,26,45,39];
random_numbers.filter(function(element){
return element % 2;
});
//[5, 7, 9, 45, 39]
var random_numbers = [2,4,5,6,7,9,34,26,45,39];
random_numbers.find(function(element){
 return element > 20;
});
//34
var random_numbers = [1,2,4,5,6,7];
function logArrayElements(element, index, array) {
  console.log('a[' + index + '] = ' + element);
}
random_numbers.forEach(logArrayElements);
//a[0] = 11
//a[1] = 21
//a[2] = 41
//a[3] = 51
//a[4] = 61
//a[5] = 71
var random_numbers = [1,2,4,5,6,7];
var doubledArray = random_numbers.map(function(element){
  return element * 2;
});
console.log(doubledArray); //[2, 4, 8, 10, 12, 14]
// Syntax - 
// array.reduce(function(total, currentValue, currentIndex, arr), initialValue);

var rockets = [
     {country:'Russia', launches:32 }, { country:'US', launches:23 },  { country:'China', launches:16 }, 
     { country:'Europe(ESA)', launches:7 }, { country:'India', launches:4 }, { country:'Japan', launches:3 } 
];  
var sum = rockets.reduce(function(total, elem) {   return total + elem.launches; }, 0);    
sum // 85

4.

toString()
valueOf()

总结

Array In ES5

参考

上一篇 下一篇

猜你喜欢

热点阅读