1、使用括号表示法访问数组的内容
let myArray = ["a", "b", "c", "d"];
// Only change code below this line
myArray[1]='not b anymore'
// Only change code above this line
console.log(myArray[0]);
2、使用 push() 和 unshift() 将项目添加到数组
push()方法将元素添加到数组的末尾,unshift()并将元素添加到开头
function mixedNumbers(arr) {
arr.unshift('I', 2, 'three')
arr.push(7, 'VIII', 9)
return arr;
}
console.log(mixedNumbers(['IV', 5, 'six']));
3、使用 pop() 和 shift() 从数组中删除项目
function popShift(arr) {
let popped=arr.pop(); // Change this line
let shifted=arr.shift(); // Change this line
return [shifted, popped];
}
console.log(popShift(['challenge', 'is', 'not', 'complete']));
4、使用 splice() 删除项目
const arr = [2, 4, 5, 1, 7, 5, 2, 1];
// Only change code below this line
arr.shift();
arr.splice(3,4)
// Only change code above this line
console.log(arr);
5、使用 splice() 添加项目
function htmlColorNames(arr) {
// Only change code below this line
arr.splice(0,2,'DarkSalmon','BlanchedAlmond')
// Only change code above this line
return arr;
}
console.log(htmlColorNames(['DarkGoldenRod', 'WhiteSmoke', 'LavenderBlush', 'PaleTurquoise', 'FireBrick']));
6、使用 slice() 复制数组项
等待:forecast应该返回[“warm”, “sunny”]
function forecast(arr) {
// Only change code below this line
arr=arr.slice(2,4)
return arr;
}
console.log(forecast(['cold', 'rainy', 'warm', 'sunny', 'cool', 'thunderstorms']));
7、使用扩展运算符复制数组
function copyMachine(arr, num) {
let newArr = [];
while (num >= 1) {
// Only change code below this line
// Only change code above this line
num--;
newArr.push([...arr])
}
return newArr;
}
console.log(copyMachine([true, false, true], 2));
8、使用扩展运算符组合数组
function spreadOut() {
let fragment = ['to', 'code'];
let sentence=["learning", ...fragment, "is", "fun"]; // Change this line
return sentence;
}
console.log(spreadOut());
9、使用 indexOf() 检查元素是否存在
function quickCheck(arr, elem) {
// Only change code below this line
let flag =arr.indexOf(elem)
if(flag==-1) return false
return true
// Only change code above this line
}
console.log(quickCheck(['squash', 'onions', 'shallots'], 'mushrooms'));
10、使用 For 循环遍历数组的所有项
function filteredArray(arr, elem) {
let newArr = [];
// Only change code below this line
for(let i=0;i<arr.length;i++){
newArr.push([...arr[i]])
for(let j=0;j<arr[i].length;j++){
if(arr[i][j]==elem){
newArr.pop([...arr[i]])
break;
}
}
}
// Only change code above this line
return newArr;
}
console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3));
11、创建复杂的多维数组
let myNestedArray = [
// Only change code below this line
["unshift", false, 1, 2, 3, "complex", "nested"],
["loop", "shift", 6, 7, 1000, "method"],
["concat", false, true, "spread", "array", ["deep"]],
["mutate", 1327.98, "splice", "slice", "push", [["deeper"]]],
["iterate", 1.3849, 7, "8.4876", "arbitrary", "depth", [[["deepest"]]]]
// Only change code above this line
];
12、向 JavaScript 对象添加键值对
let foods = {
apples: 25,
oranges: 32,
plums: 28
};
// Only change code below this line
foods.bananas=13;
foods.grapes=35;
foods.strawberries=27;
// Only change code above this line
console.log(foods);
13、修改嵌套在对象内的对象
let userActivity = {
id: 23894201352,
date: 'January 1, 2017',
data: {
totalUsers: 51,
online: 42
}
};
// Only change code below this line
userActivity.data.online=45
// Only change code above this line
console.log(userActivity);
14、使用括号表示法访问属性名称
let foods = {
apples: 25,
oranges: 32,
plums: 28,
bananas: 13,
grapes: 35,
strawberries: 27
};
function checkInventory(scannedItem) {
// Only change code below this line
return foods[scannedItem]
// Only change code above this line
}
console.log(checkInventory("apples"));
15、使用 delete 关键字删除对象属性
let foods = {
apples: 25,
oranges: 32,
plums: 28,
bananas: 13,
grapes: 35,
strawberries: 27
};
// Only change code below this line
delete foods.oranges;
delete foods.plums;
delete foods.strawberries;
// Only change code above this line
console.log(foods);
16、检查对象是否具有属性
let users = {
Alan: {
age: 27,
online: true
},
Jeff: {
age: 32,
online: true
},
Sarah: {
age: 48,
online: true
},
Ryan: {
age: 19,
online: true
}
};
function isEveryoneHere(userObj) {
// Only change code below this line
if('Alan' in userObj && 'Jeff' in userObj && 'Sarah' in userObj && 'Ryan' in userObj){
return true
}else{
return false
}
// Only change code above this line
}
console.log(isEveryoneHere(users));
17、使用 for…in 语句遍历对象的键
const users = {
Alan: {
online: false
},
Jeff: {
online: true
},
Sarah: {
online: false
}
}
function countOnline(usersObj) {
// Only change code below this line
let flag=0;
for (let value in usersObj) {
// console.log(usersObj[user]['online'])
if(usersObj[value].online)
flag++;
}
return flag
// Only change code above this line
}
console.log(countOnline(users));
18、使用 Object.keys() 生成所有对象键的数组
let users = {
Alan: {
age: 27,
online: false
},
Jeff: {
age: 32,
online: true
},
Sarah: {
age: 48,
online: false
},
Ryan: {
age: 19,
online: true
}
};
function getArrayOfUsers(obj) {
// Only change code below this line
return Object.keys(obj)
// Only change code above this line
}
console.log(getArrayOfUsers(users));
19、修改存储在对象中的数组
let user = {
name: 'Kenneth',
age: 28,
data: {
username: 'kennethCodesAllDay',
joinDate: 'March 26, 2016',
organization: 'freeCodeCamp',
friends: [
'Sam',
'Kira',
'Tomo'
],
location: {
city: 'San Francisco',
state: 'CA',
country: 'USA'
}
}
};
function addFriend(userObj, friend) {
// Only change code below this line
userObj.data.friends.push(friend)
return userObj.data.friends
// Only change code above this line
}
console.log(addFriend(user, 'Pete'));