数据结构基础

数据结构基础主要讲了数组和对象的特性和基本方法。

  • 数组

    • 获取数组元素的方法array[index]
    • 添加删除元素
      • array.push(ele) 数组末尾添加ele
      • array.shift(ele) 数组开头添加ele
      • array.pop() 删除数组末尾元素
      • array.unshift() 删除数组开头元素
      • array.splice(index, len) 删除数组从位于index开始的的元素,删除长度为len
      • array.splice(index, len, ele) 删除数组从位于index开始的元素,删除长度为len,并添加ele
    • 扩展运算符[...arr]
      • 利用扩展运算符赋值数组
      • 利用扩展运算符合并数组
    • 检测数组是否包含某个元素array.indexOf(ele),如果包含,则返回ele所在的index,如果不包含,返回-1
    • 利用for循环遍历数组for(let i = 0; i < len; i ++)
    • 多维数组
  • Object

    • 多维Object
    • 获取对象的值
      • obj.key
      • obj["key"]
    • 删除对象的值 delete obj.key
    • 检测对象是否包含某个属性 obj.hasOwnProperty(key)
    • 利用for…in循环遍历对象for(let i in obj)
    • 获取对象的所有属性 Object.keys(obj),返回一个全是对象属性的数组

这一部分内容比较基础,难度不大。以下是这部分的习题解答:


Introduction to the Basic Data Structure Challenges

  • Use an Array to Store a Collection of Data

    1
    let yourArray = [true, "abc", 12, [1, 2], {a: 1, b: 2}]; // change this line
  • Access an Array’s Contents Using Bracket Notation

    1
    2
    3
    4
    5
    let myArray = ["a", "b", "c", "d"];
    // change code below this line
    myArray[1] = "hello";
    //change code above this line
    console.log(myArray);
  • Add Items to an Array with push() and unshift()

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    function mixedNumbers(arr) {
    // change code below this line
    arr.unshift('I', 2, 'three');
    arr.push(7, 'VIII', 9);
    // change code above this line
    return arr;
    }

    // do not change code below this line
    console.log(mixedNumbers(['IV', 5, 'six']));
  • Remove Items from an Array with pop() and shift()

    1
    2
    3
    4
    5
    6
    7
    8
    function popShift(arr) {
    let popped = arr.pop(); // change this line
    let shifted = arr.shift(); // change this line
    return [shifted, popped];
    }

    // do not change code below this line
    console.log(popShift(['challenge', 'is', 'not', 'complete']));
  • Remove Items Using splice()

    1
    2
    3
    4
    5
    6
    7
    8
    9
    function sumOfTen(arr) {
    // change code below this line
    arr.splice(1,2);
    // change code above this line
    return arr.reduce((a, b) => a + b);
    }

    // do not change code below this line
    console.log(sumOfTen([2, 5, 1, 5, 2, 1]));
  • Add Items Using splice()

    1
    2
    3
    4
    5
    6
    7
    8
    9
    function htmlColorNames(arr) {
    // change code below this line
    arr.splice(0, 2, "DarkSalmon", "BlanchedAlmond");
    // change code above this line
    return arr;
    }

    // do not change code below this line
    console.log(htmlColorNames(['DarkGoldenRod', 'WhiteSmoke', 'LavenderBlush', 'PaleTurqoise', 'FireBrick']));
  • Copy Array Items Using slice()

    1
    2
    3
    4
    5
    6
    7
    function forecast(arr) {
    // change code below this line
    return arr.slice(2, 4);
    }

    // do not change code below this line
    console.log(forecast(['cold', 'rainy', 'warm', 'sunny', 'cool', 'thunderstorms']));
  • Copy an Array with the Spread Operator

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    function copyMachine(arr, num) {
    let newArr = [];
    while (num >= 1) {
    // change code below this line
    newArr.push([...arr]);
    // change code above this line
    num--;
    }
    return newArr;
    }

    // change code here to test different cases:
    console.log(copyMachine([true, false, true], 2));
  • Combine Arrays with the Spread Operator

    1
    2
    3
    4
    5
    6
    7
    8
    function spreadOut() {
    let fragment = ['to', 'code'];
    let sentence = ['learning', ...fragment, 'is', 'fun']; // change this line
    return sentence;
    }

    // do not change code below this line
    console.log(spreadOut());
  • Check For The Presence of an Element With indexOf()

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    function quickCheck(arr, elem) {
    // change code below this line
    if(arr.indexOf(elem) === -1) {
    return false;
    }
    return true;
    // change code above this line
    }

    // change code here to test different cases:
    console.log(quickCheck(['squash', 'onions', 'shallots'], 'mushrooms'));
  • Iterate Through All an Array’s Items Using For Loops

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    function filteredArray(arr, elem) {
    let newArr = [];
    // change code below this line
    for(let i = 0; i < arr.length; i ++) {
    if(arr[i].indexOf(elem) === -1){
    newArr.push(arr[i]);
    }
    }
    // change code above this line
    return newArr;
    }

    // change code here to test different cases:
    console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3));
  • Create complex multi-dimensional arrays

    1
    2
    3
    4
    5
    6
    7
    8
    9
    let myNestedArray = [
    // change code below this line
    ['unshift', false, 1, 2, 3, 'complex', 'nested',['deep', ['deeper', ['deepest']]]],
    ['loop', 'shift', 6, 7, 1000, 'method'],
    ['concat', false, true, 'spread', 'array'],
    ['mutate', 1327.98, 'splice', 'slice', 'push'],
    ['iterate', 1.3849, 7, '8.4876', 'arbitrary', 'depth']
    // change code above this line
    ];
  • Add Key-Value Pairs to JavaScript Objects

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    let foods = {
    apples: 25,
    oranges: 32,
    plums: 28
    };

    // change code below this line
    foods.bananas = 13;
    foods.grapes = 35;
    foods["strawberries"] = 27;
    // change code above this line

    console.log(foods);
  • Modify an Object Nested Within an Object

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    let userActivity = {
    id: 23894201352,
    date: 'January 1, 2017',
    data: {
    totalUsers: 51,
    online: 42
    }
    };

    // change code below this line
    userActivity.data.online = 45;
    // change code above this line

    console.log(userActivity);
  • Access Property Names with Bracket Notation

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    let foods = {
    apples: 25,
    oranges: 32,
    plums: 28,
    bananas: 13,
    grapes: 35,
    strawberries: 27
    };
    // do not change code above this line

    function checkInventory(scannedItem) {
    // change code below this line
    return foods[scannedItem];
    }

    // change code below this line to test different cases:
    console.log(checkInventory("apples"));
  • Use the delete Keyword to Remove Object Properties

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    let foods = {
    apples: 25,
    oranges: 32,
    plums: 28,
    bananas: 13,
    grapes: 35,
    strawberries: 27
    };

    // change code below this line
    delete foods.oranges;
    delete foods.plums;
    delete foods.strawberries;
    // change code above this line

    console.log(foods);
  • Check if an Object has a Property

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    let users = {
    Alan: {
    age: 27,
    online: true
    },
    Jeff: {
    age: 32,
    online: true
    },
    Sarah: {
    age: 48,
    online: true
    },
    Ryan: {
    age: 19,
    online: true
    }
    };

    function isEveryoneHere(obj) {
    // change code below this line
    if(obj.hasOwnProperty('Alan') && obj.hasOwnProperty('Jeff') && obj.hasOwnProperty('Sarah') && obj.hasOwnProperty('Ryan')) {
    return true;
    }
    return false;
    // change code above this line
    }

    console.log(isEveryoneHere(users));
  • Iterate Through the Keys of an Object with a for…in Statement

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    let users = {
    Alan: {
    age: 27,
    online: false
    },
    Jeff: {
    age: 32,
    online: true
    },
    Sarah: {
    age: 48,
    online: false
    },
    Ryan: {
    age: 19,
    online: true
    }
    };

    function countOnline(obj) {
    // change code below this line
    let result = 0;
    for(let i in obj) {
    if(obj[i].online === true) {
    result =result +=1;
    }
    }
    return result;
    // change code above this line
    }

    console.log(countOnline(users));
  • Generate an Array of All Object Keys with Object.keys()

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    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) {
    // change code below this line
    return Object.keys(obj);
    // change code above this line
    }

    console.log(getArrayOfUsers(users));
  • Modify an Array Stored in an Object

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    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) {
    // change code below this line
    userObj.data.friends.push(friend);
    return userObj.data.friends;
    // change code above this line
    }

    console.log(addFriend(user, 'Pete'));