为数组 arr 中的每个元素求二次方。不要直接修改数组 arr,结果返回新的数组
示例:
输入 [1, 2, 3, 4]
输出 [1, 4, 9, 16]
function square(arr) {
return arr.map(function(item){
return item*item;
})
}
var arr = [
{name:'zopp',age:0},
{name:'gpp',age:18},
{name:'yjj',age:8}
];
function compare(property){
return function(a,b){
return a[property]-b[property];
}
}
console.log(arr.sort(compare('age')));
var arr = ['a', 'b', 'a', 'c', 'd', 'c'];
//方法一:reduce统计字符个数,filter过滤个数>1的数组
function duplicates(arr) {
var temp = arr.reduce(function(res, value) {
res[value] ? res[value]++ : res[value] = 1;
return res;
}, {});
var newArr = [];
Object.keys(temp).forEach(function(value) {
if (temp[value] > 1) {
newArr.push(value);
}
})
return newArr;
}
//方法二:借用indexOf和lastIndexOf
function duplicates2(arr) {
var temp = [];
arr.forEach(function(value) {
if (arr.indexOf(value) === arr.lastIndexOf(value) && temp.indexOf(value) === -1) {
temp.push(value);
}
});
return temp;
}
// var newArr = duplicates(arr);
// console.log(newArr);
另辟一篇博客
function quickSort(arr, l, r) {
if (l < r) {
var i = l,
j = r,
x = arr[i];
while (i < j) {
while (i < j && arr[j] > x) {
j--;
}
if (i < j) {
arr[i++] = arr[j];
}
while (i < j && arr[i] < x) {
i++;
}
if (i < j) {
arr[j--] = arr[i];
}
}
arr[i] = x;
quickSort(arr, l, i - 1);
quickSort(arr, i + 1, r);
}
}
//测试用例
var arr = [4,6,2,5,1,3];
quickSort(arr, 0 , arr.length-1);
console.log(arr);
//[1, 2, 3, 4, 5, 6]
function merge(leftArr, rightArr) {
var result = [];
while (leftArr.length && rightArr.length) {
if (leftArr[0] < rightArr[0])
result.push(leftArr.shift()); //把最小的最先取出,放到结果集中
else
result.push(rightArr.shift());
}
return result.concat(leftArr, rightArr); //剩下的就是合并,这样就排好序了
}
function mergeSort(arr) {
if (arr.length == 1) //递归结束条件
return arr;
var mid = Math.floor(arr.length / 2); //求出中点
var left = arr.slice(0, mid); //分割数组
var right = arr.slice(mid);
return merge(mergeSort(left), mergeSort(right)); //递归合并与排序
}
//测试用例:
var arr = mergeSort([32, 12, 56, 78, 76, 45, 36]);
console.log(arr);
function search(arr, key) {
var low = 0,
high = arr.length - 1;
while (low <= high) {
var mid = parseInt((low + high) / 2);
if (key == arr[mid]) {
return mid;
} else if (key > arr[mid]) {
low = mid + 1;
} else if (key < arr[mid]) {
high = mid - 1;
}
}
return -1;
}
//测试代码
var arr = [1,2,3,4,5,6];
console.log(search(arr,1));
console.log(search(arr,4));
//方法一
function strMaxCount1(str) {
var obj = {},
maxCount = 0,
resChar = '';
if (str == null || str.length == 0) {
return;
}
for (var i = 0, len = str.length; i < len; i++) {
var key = str.charAt(i) + '';
if (obj.hasOwnProperty(key)) {
obj[key]++;
} else {
obj[key] = 1;
}
if (obj[key] > maxCount) {
maxCount = obj[key];
resChar = key;
}
}
return resChar;
}
//方法二
function strMaxCount(str) {
var obj = str.split('').reduce(function(res, value, key, arr) {
res[value] ? res[value]++ : res[value] = 1;
return res;
}, {});
var count=0;
c = '';
for(var k in obj){
if(obj.hasOwnProperty(k)){
if(obj[k]>count){
count = obj[k];
c = k;
}
}
}
return c;
}
console.log(strMaxCount('abbbdabs'));
console.log(strMaxCount1('abbbdabs'));
var a= { n: 1 };
var b= foo;
a.x = a= { n: 2 };
console.log("a",a);
console.log("b", b);
console.log("a.x", a.x);
console.log("b.x", b.x);
a:{n:2}
b:{n:1,x:{n:2}}
a.x undefined
b.x {n:2}
注意js中的连等运算符,和java不一样,并不是从右向左赋值的。
// a.x = a = {n:2}相当于
a.x = a
a.x = {n:2}
a = {n:2}
详细解释:
var a = {n:1}; /*定义a,赋值为{n:1},假设指向存储地址为add_1*/
var b = a; /*定义b,赋值为a,指向同一存储地址add_1*/
a.x = a = {n:2};
/*(1):定义a.x,赋值为a,即{n:1},存储地址add_1上面的内容被更改
(2):a.x重新赋值为{n:2},存储地址add_1上面的内容被更改
(3):a重新赋值为{n:2},更改存储地址add_2*/
alert(a.x);
/*现在a的存储地址add_2,内容为{n:2},上面并不存在a.x属性,所以为undefined*/
alert(b.x);
/*现在b的存储地址add_1,内容为{n:1,x:{n:2}},所以b.x为{n:2}*/
解释的精辟,敬佩!参考链接:https://segmentfault.com/q/1010000002637728
// 例如:[1, 2, 3, 4, 5, 9], 指定值为12, 结果为true
function countAdd() {
var count = 0,
timer = null;
timer = setInterval(function() {
if (count < 5) {
count++;
console.log(new Date,count);
} else {
clearInterval(timer);
}
}, 1000);
}
countAdd();
//方法二
function countAdd() {
var count = 0,
timer = null;
function fn() {
if (count < 5) {
count++;
console.log(new Date, count);
setTimeout(fn, 1000);
} else {
clearTimeout(timer);
}
}
timer = setTimeout(fn, 1000);
}
countAdd();