JS数组常⽤⽅法练习题
1.将 border-left-width 转换成 borderLeftWidth (原创)
/* 将 border-left-width 转换成 borderLeftWidth */
function camelize(str){
let arr=str.split('');
for(let i=0;i<arr.length;i++){
if(arr[i]=='-'){
arr[i+1]=arr[i+1].toUpperCase();
arr.splice(i,1);
}
}
return arr.join('');
}
console.log(camelize(' border-left-width'));
2. 写⼀个函数 filterRange(arr, a, b) 获取⼀个数组 arr,查⼤于等于 a ⽽⼩于 b 之间的元素并返回它们的数组。// 写⼀个函数 filterRange(arr, a, b) 获取⼀个数组 arr,查⼤于等于 a ⽽⼩于 b 之间的元素并返回它们的数组。
function filterRange(arr,a,b){
return arr.filter(item=>{
let arr2=(item<4)&&(item>=1);
return arr2;
});
}
console.log(filterRange([5,3,8,1],1,4));
3.复制和排序数组,不影响原数组 (原创)
/* 复制和排序数组,不影响原数组 */
let arr=['dahfk','akfgli','uigh'];
let sorted=copySorted(arr);
console.log(sorted);
console.log(arr)
function copySorted(arr){
let arr2=[];
for(let i=0;i<arr.length;i++){
arr2[i]=arr[i];
}
return arr2.sort();
}
4.你有⼀个 users 对象数组,每个对象都有 user.name。编写将其转换为 names 数组的代码。
/* 你有⼀个 users 对象数组,每个对象都有 user.name。编写将其转换为 names 数组的代码。 */
let john ={ name:"John", age:25};
let pete ={ name:"Pete", age:30};
let mary ={ name:"Mary", age:28};
let users =[ john, pete, mary ];
let names = users.map(item=>item.name);
console.log( names );// John, Pete, Mary
5.有⼀个 user 对象数组,每个对象都有 name,surname 和 id。
编写代码以从中创建另⼀个具有 id 和 fullName 的对象,其中 fullName 由 name 和 surname ⽣成。
/* 有⼀个 user 对象数组,每个对象都有 name,surname 和 id。
编写代码以从中创建另⼀个具有 id 和 fullName 的对象,其中 fullName 由 name 和 surname ⽣成。 */
let john ={ name:"John", surname:"Smith", id:1};
let pete ={ name:"Pete", surname:"Hunt", id:2};
let mary ={ name:"Mary", surname:"Key", id:3};
let users =[ john, pete, mary ];
let usersMapped = users.map(user =>({//是因为要循环吗?所以{}外加()
fullName:`${user.name}${user.surname}`,
id: user.id
}));
/*
usersMapped = [
{ fullName: "John Smith", id: 1 },
{ fullName: "Pete Hunt", id: 2 },
{ fullName: "Mary Key", id: 3 }
]
*/
console.log( usersMapped[0].id );// 1
console.log( usersMapped[0].fullName );// John Smith
6. 编写函数 sortByAge(users) 获得对象数组的 age 属性并对它进⾏排序。
/
* 编写函数 sortByAge(users) 获得对象数组的 age 属性并对它进⾏排序。 */
let john ={ name:"John", age:25};
let pete ={ name:"Pete", age:30};
let mary ={ name:"Mary", age:28};
let arr =[ pete, john, mary ];
sortByAge(arr);
function sortByAge(arr){
arr.sort((a,b)=>a.age>b.age?1:-1);
}
// now: [john, mary, pete]
console.log(arr[0].name);// John
console.log(arr[1].name);// Mary
console.log(arr[2].name);// Pete
8.编写 getAverageAge(users) 函数,该函数获取⼀个具有 age 属性的对象数组,并获取平均值。⽅法1(原创)
/* 编写 getAverageAge(users) 函数,该函数获取⼀个具有 age 属性的对象数组,并获取平均值。 */
let john ={ name:"John", age:25};
let pete ={ name:"Pete", age:30};
let mary ={ name:"Mary", age:29};
let arr =[ john, pete, mary ];
console.log(getAverageAge(arr));
// ---------------------------------
// function getAverageAge(arr) {js方法
//    let ages=arr.map(item=>item.age);
//    let sums=0;
//    for(let num of ages){
//        sums+=(+num);
//    }
//    return sums/arr.length;
// }
// --------------------------------⽅法2
function getAverageAge(arr){
duce((sum,current)=>sum+current.age,0)/arr.length;
}
9.创建⼀个函数 unique(arr),返回去除重复元素的 arr。
/* 创建⼀个函数 unique(arr),返回去除重复元素的 arr。 */
let strings =["Hare","Krishna","Hare","Krishna", "Krishna","Krishna","Hare","Hare",":-O"
];
console.log(unique(strings));// Hare, Krishna, :-O
// ---------------------------------------??
//  function unique(arr) {
//    for(let i=0;i<strings.length-1;i++){
//        for(let j=i+1;j<strings.length;j++){
//            if(arr[i]==arr[j]){}
//                arr.splice(j,1);
/
/                strings.length--;
//            }
//        }
//    }
//    return arr;
//    }
//    -------------------------------
function unique(arr){
let result=[];
for(let str of arr){
if(!result.includes(str)){
result.push(str);
}
}
return result;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。