js 之map和reduce方法 练习
2017-08-14 本文已影响47人
赵仝
下面是js中MapReduce的简单练习。主要使用js提供给数组的map和reduce方法,最后一个例子考虑一下可以用java多线程来实现。
var arr1 = [1, 2, 3, 4, 5, 6];
var arr11 = arr1.map(x => {
return x * x;
});
//console.log(arr11);
//将数组中的数字转化为字符串
var arr2 = [1, 2, 3, 4, 5];
var arr22 = arr2.map(String);
//console.log(arr22);
// 利用 reduce进行数组求和
var arr3 = [1, 2, 3, 4, 5, 6];
var arr33 = arr3.reduce((x, y) => {
return x + y;
})
//console.log(arr33);
//实现一个将一个字符串转化为数字的函数
var str2Int = x => {
var result;
if (typeof (x) == 'string') {
var arr = new Array();
for (var i = 0; i < x.length; i++) {
arr.push(x.charAt(i));
}
result = arr.map(x => {
return x - '0';
}).reduce((x, y) => {
return x * 10 + y;
});
}
return result;
}
console.log(str2Int("4545454"));
//使用map reduce 统计出现的单词数
var job = {
data: [
"We are glad to see you here. This site is dedicated to",
"poetry and to the people who make poetry possible",
"poets and their readers. FamousPoetsAndPoems.com is",
"a free poetry site. On our site you can find a large",
"collection of poems and quotes from over 631 poets",
"Read and Enjoy Poetry",
"I, too, sing America",
"I am the darker brother",
"They send me to eat in the kitchen",
"When company comes",
"But I laugh",
"And eat well",
"And grow strong",
"Tomorrow",
"Ill be at the table",
"When company comes",
"Nobodyll dare",
"Say to me",
"Eat in the kitchen",
"Then",
"Besides",
"Theyll see how beautiful I am",
"And be ashamed",
"I, too, am America"
],
//map 函数
map: x => {
var arr1 = x.split(" ");
var temp = [];
for (var i = 0; i < arr1.length; i++) {
temp.push({ key: arr1[i], value: 1 });
}
return temp;
},
//reduce函数
reduce: x => {
var result = {};
for (var i = 0; i < x.length; i++) {
var step = x[i];
result[step.key] = result[step.key] ? (result[step.key] + 1) : 1;
}
return result;
},
start: () => {
var alldata = [];
for (var i = 0; i < job.data.length; i++) {
alldata.push.apply(alldata, job.map(job.data[i]));
}
var result = job.reduce(alldata);
console.log(JSON.stringify(result));
}
};
job.start();
运行结果:
ceshi.gif<iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=530 height=86 src="//music.163.com/outchain/player?type=2&id=28754873&auto=1&height=66"></iframe>