Javascript数据结构算法之数组基础篇

数组

一个存储元素的线性集合。

1. 创建数组

var objects = [1, "joe", true, null];

isArray()
Array.isArray(objects);

2. 读写数组

数组的长度可以任意增长。

3. 由字符串生成数组

split()

var sentence = "the quick brown fox jumped over the lazy pig";
var words = sentence.split(" ");
for(var i = 0; iprint("word"+i+":"+words[i]);
}
4. 查找元素

indexOf()
lastIndexOf()

var names=["David", "Cynthia", "Raymond", "Clayton", "Jennifer", "David"];
var name = "David";
var firstposition = names.indexOf(name);
var lastposition = names.lastIndexOf(name);
if(firstposition>=0)
{print("Found "+name+" at positon "+firstposition);if(lastposition>=0)print("Last found "+name+" at positon "+lastposition);
}
else
{print(name+"not found in array");
}
5. 数组的字符串表示

join()

var names=["David", "Cynthia", "Raymond", "Clayton", "Jennifer"]
print(names);
var namestr = names.join();
print(namestr);
namestr = names.toString();
print(namestr);

the results are the same: David,Cynthia,Raymond,Clayton,Mike,Jennifer

6. 由已有数组创建新数组

concat()
splice()

var cisDept = [1,2,3,4,5];
var dmpDept = [6,7,8];
var itDiv = cisDept.concat(dmpDept);
print(itDiv);
itDiv = dmpDept.concat(cisDept);
print(itDiv);

the results is:
1,2,3,4,5,6,7,8
6,7,8,1,2,3,4,5

var itDiv = [1,2,3,4,5,6,7,8];
var cisDept = itDiv.splice(3,3);
print(cisDept);
print(itDiv);

the results is:
4,5,6
1,2,3,7,8

7. 可变函数

push()
unshift()
pop()
shift()
reverse()
sort()

var nums = [3,4,5];
nums.push(6);
nums.unshift(1,2);
print(nums);
nums.reverse();
print(nums); var diso_nums = [3,1,2,100,4,200];
function compare(num1, num2){return num1 - num2;
}
diso_nums.sort(compare);
print(diso_nums);

the results are:
1,2,3,4,5,6
6,5,4,3,2,1
1,2,3,4,100,200

8. 迭代器方法

forEach()
every()
some()
reduce()
reduceRight()
:返回一个值,或者执行某个操作

function square(num){print(num, num*num);
}
function isEven(num){return num % 2 == 0;
}
function concat(accumulatedString, item){return accumulatedString + item;
}var nums1 = [1,2,3,4,5];
var nums2 = [2,4,6,8,10];
var nums3 = [1,2,3,5,7];
var words = ["the ", "quick ", "brown ", "fox "];nums1.forEach(square);//对数组中每个元素都是用Squre()方法
nums2.every(isEven); //true
nums3.some(isEven);  //true
words.reduce(concat);  //累加:"the quick brown fox"
words.reduceRight(concat);  //反向累加:"fox brown quick the"

map()
filter()
:返回一个数组

function curve(grade){return grade + 5;
}var grades = [77, 65, 81, 92, 83];
var nums = [2,4,5,6,8,10];
var newgrades = grades.map(curve);
var evens = nums.filter(isEven);
function afterc(str){if(str.indexOf("cie") > -1 ){return true;}return false;
}var words = ["recieve", "deceive", "percieve", "deceit", "concieve"];
var rightwords = words.filter(afterc);
9. 二维数组
10. 参差不齐的数组
11. 对象数组
function weekTemps(){this.dataStore = [];this.add = add;this.average = average;
}
function add(temp)
{this.dataStore.push(temp);
}
function average()
{var total;for(var i=0; i<this.dataStore.length; i++){total += this.dataStore[i];}return total / this.dataStore.length;
}var thisWeek = new weekTemps();
thisWeek.add(52);
thisWeek.add(62);
thisWeek.add(55);
thisWeek.add(61);
thisWeek.add(50);
thisWeek.add(49);
thisWeek.add(59);
print(thisWeek.average());


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部