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'));
  1. 写一个函数 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
  1. 编写函数 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) {
//     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) {
    return arr.reduce((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;
}
  • 2
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值