JavaScript 数组

JavaScript 数组成员方法

  • concat
  • entries
  • every
  • filter
  • forEach
  • indexOf
  • join
  • keys
  • lastIndexOf
  • map
  • pop
  • push
  • reduce
  • reduceRight
  • reverse
  • shift
  • slice
  • some
  • sort
  • splice
  • toLocaleString
  • toString
  • unshift

concat

var a = [1];
a.concat(2); // [1, 2]
a.concat([2]); // [1, 2]
a.concat([[2]]); // [1, [2]]
// a is [1]

entries

var a = ['a', 'b', 'c'];
var iterator = a.entries(); // iterator is an ArrayIterator
var row;
do {
	row = iterator.next();
	if (row.done) break;

	console.log(row.value, row.done); // row.value = [index, value]
	// [0, 'a'], false
	// [0, 'b'], false
	// [0, 'c'], false
} while (true);

every、some、forEach

var a = [1, 2, 3, 4];
var callback_context = this;
a.every(function(element, index, array) {
	// return false to break the iterate and return false
	return element > 2;
}, callback_context); // false

var a = [1, 2, 3, 4];
var callback_context = this;
a.some(function(element, index, array) {
	// return true to break the iterate and return true
	return element > 2;
}, callback_context); // true

var a = [1, 2, , 4];
var callback_context = this;
a.forEach(function(element, index, array) {
	// called once for every not undefined element
}, callback_context); // true
var b = [];
a.forEach(function(element) {
	b.push(element);
});
// a is [1, 2, undefined, 4]
// b is [1, 2, 4]

filter

var a = [1, 2, 3, 4];
var callback_context = this;
a.filter(function(element, index, array) {
	if (element % 2) {
		return true;
	}
	return false;
}, callback_context); // [1, 3]
// a is [1, 2, 3, 4]

keys

var a = ['a', 'b', 'c'];
var iterator = a.keys();
var row;
do {
	row = iterator.next();
	if (row.done) break;

	console.log(row.value, row.done); // row.value = index
	// 0, false
	// 1, false
	// 2, false
} while (true);

map

var a = [1, 2, 3, 4];
var callback_context = this;
a.map(function(element, index, array) {
	return element * 2;
}, callback_context); // [2, 4, 6, 8]
// a is [1, 2, 3, 4]

push

var a = ['a'];
a.push('b'); // 2
// a is ['a', 'b']
a.push(['c']); // 3
// a is ['a', 'b', ['c']]

reduce、reduceRight

var a = [1, 2, 3, 4];
a.reduce(function(precious_value, current_value, current_index) {
	return precious_value + current_value;
}); // 10
// precious_value = 1, current_value = 2 precious_value + current_value = 3
// precious_value = 3, current_value = 3 precious_value + current_value = 6
// precious_value = 6, current_value = 4 precious_value + current_value = 10
// 10

var a = [1, 2, 3, 4];
a.reduce(function(precious_value, current_value, current_index) {
	return precious_value + current_value;
}, 5); // 5 is initial value
// precious_value = 5, current_value = 1 precious_value + current_value = 6
// precious_value = 6, current_value = 2 precious_value + current_value = 8
// precious_value = 8, current_value = 3 precious_value + current_value = 11
// precious_value = 11, current_value = 4 precious_value + current_value = 15
// 15

// reduceRight performs same as reduce
var a = [1, 2, 3, 4];
a.reduce(function(precious_value, current_value, current_index) {
	return precious_value + current_value;
}); // 10
// precious_value = 4, current_value = 3 precious_value + current_value = 7
// precious_value = 7, current_value = 2 precious_value + current_value = 9
// precious_value = 9, current_value = 1 precious_value + current_value = 10
// 10

sort

var a = [4, 3, 2, 1];
a.sort(); // [1, 2, 3, 4]
// a is [1, 2, 3, 4]

var a = ['d', 'c', 'b', 'a'];
a.sort(function(item1, item2) {
	if (item1 == item2) {
		return 0;
	}
	if (item1.charCodeAt(0) > item2.charCodeAt(0)) {
		return 1;
	}
	// item1 less than item2
	return -1;
	// JavaScript 采取的算法应该是冒泡排序
});

splice

var a = ['a', 'b', 'c'];
a.splice(0, 1); // ['a'] splice(start, delete_count)
// a is ['b', 'c']

var a = ['a', 'b', 'c'];
a.splice(0, 1, '1'); // ['a'] splice(start, delete_count, replace_element)
// a is ['1', 'b', 'c']

var a = ['a', 'b', 'c'];
a.splice(0, 2, '1', '2'); // ['a', 'b'] splice(start, delete_count, replace_element_1, replace_element_2)
// a is ['1', '2', 'c']

var a = ['a', 'b', 'c'];
a.splice(0, 1, '1', '2'); // ['a'] splice(start, delete_count, replace_element_1, replace_element_2)
// a is ['1', '2', 'b', 'c']

JavaScript Array 还有一些方法,但由于浏览器兼容性,即使如 entries 也较少使用。

作者: 袖之欢

科技改变生活,编程改变世界。

发表评论

电子邮件地址不会被公开。 必填项已用*标注