How To Use Array

/** * 1. forEach() */ var arr1 = [1,2,3,4,5,7]; arr1.forEach(function(value,index){ console.log('第'+index+'个'+value) }); ['_', 't', 'a', 'n', 'i', 'f', ']'].forEach(function (value, index, array) { this.push(String.fromCharCode(value.charCodeAt() + index + 2)) }, out = []) console.log(out.join('')); // <- 'awesome' /** * 2. some 、 every */ var max = -Infinity satisfied = [10, 12, 10, 8, 5, 23].some(function (value, index, array) { if (value > max) max = value return value < 10 }) console.log(max) // <- 12 console.log(satisfied); // <- true var max = -Infinity satisfied = [10, 12, 10, 8, 5, 23].every(function (value, index, array) { if (value > max) max = value return value < 10 }) console.log(max) // <- 10 console.log(satisfied); /** * 3. join、 concat */ var a = { foo: 'bar' } var b = [1, 2, 3, a] var c = b.concat() console.log(b === c) // <- false b[3] === a && c[3] === a // <- true console.log(b); /** * 4. 栈和队列.pop,.push,.shift和.unshift */ /*后进先出*/ function Stack () { this._stack = [] } Stack.prototype.next = function () { return this._stack.pop() } Stack.prototype.add = function () { return this._stack.push.apply(this._stack, arguments) } stack = new Stack() stack.add(1,2,3) console.log(stack.next() ); // <- 3 /*先进先出*/ function Queue () { this._queue = [] } Queue.prototype.next = function () { return this._queue.shift() } Queue.prototype.add = function () { return this._queue.unshift.apply(this._queue, arguments) } queue = new Queue() console.log(queue) queue.next() // <- 1 // list = [1,2,3,4,5,6,7,8,9,10] while (item = list.shift()) { console.log(item) } console.log(list) // <- [] // /** * 5. 模型映射 .map */ values = [void 0, null, false, ''] values[7] = void 0 result = values.map(function(value, index, array){ console.log(value) return value }) // <- [undefined, null, false, '', undefined × 3, undefined] // // 遍历 var arr2 = ''; (['1', '2', '30', '9']).map(function (value) { return parseInt(value, 10) }); // 1, 2, 30, 9 [97, 119, 101, 115, 111, 109, 101].map(String.fromCharCode).join('') // <- 'awesome' // 一个映射新对象的通用模式 items.map(function (item) { return { id: item.id, name: computeName(item) } }) /** * 6. filter */ [void 0, null, false, '', 1].filter(function (value) { return value }) // <- [1] [void 0, null, false, '', 1].filter(function (value) { return !value }) // <- [void 0, null, false, ''] // /** * 7. sort */ [9,80,3,10,5,6].sort() // <- [10, 3, 5, 6, 80, 9] [9,80,3,10,5,6].sort(function (a, b) { return a - b }) // <- [3, 5, 6, 9, 10, 80] /** * 8. reduce reduceright */ Array.prototype.sum = function () { return this.reduce(function (partial, value) { return partial + value }, 0) }; [3,4,5,6,10].sum() // <- 28 // function concat (input) { return input.reduce(function (partial, value) { if (partial) { partial += ', ' } return partial + value }, '') } concat([ { name: 'George' }, { name: 'Sam' }, { name: 'Pear' } ]) // <- 'George, Sam, Pear' /** * 9. slice() */ //类数组对象转换为真正的数组。 Array.prototype.slice.call({ 0: 'a', 1: 'b', length: 2 }) // <- ['a', 'b'] /*demo 1*/ function format (text, bold) { if (bold) { text = '<b>' + text + '</b>' } var values = Array.prototype.slice.call(arguments, 2) values.forEach(function (value) { text = text.replace('%s', value) }) return text } format('some%sthing%s %s', true, 'some', 'other', 'things') // <- <b>somesomethingother things</b> /*demo 2*/ var source = [1,2,3,8,8,8,8,8,9,10,11,12,13] var spliced = source.splice(3, 4, 4, 5, 6, 7) console.log(source) // <- [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ,13] spliced // <- [8, 8, 8, 8] /*demo 3*/ var source = [1,2,3,8,8,8,8,8,9,10,11,12,13] var spliced = source.splice(9) spliced.forEach(function (value) { console.log('removed', value) }) // <- removed 10 // <- removed 11 // <- removed 12 // <- removed 13 console.log(source) // <- [1, 2, 3, 8, 8, 8, 8, 8, 9] // /** * 10. indexOf() */ var a = { foo: 'bar' } var b = [a, 2] console.log(b.indexOf(1)) // <- -1 console.log(b.indexOf({ foo: 'bar' })) // <- -1 console.log(b.indexOf(a)) // <- 0 console.log(b.indexOf(a, 1)) // <- -1 b.indexOf(2, 1) // <- 1 /** * 11. in 操作符 */ var a = [1, 2, 5] 1 in a // <- true, 但 是因为 2! 5 in a // <- false // var a = [3, 7, 6] 1 in a === !!a[1] // <- true // /** * 12. reverse() */ var a = [1, 1, 7, 8] a.reverse() // [8, 7, 1, 1]

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.