Study Notes

Study Notes

  • Source
  • WebDesign
  • Javascript

›Intermediate Algorithm Script

Basic Javascript

  • Basic JavaScript Overview

ES6

  • ES6 Overview

Regular expression

  • Regular Expression Overview
  • Lookahead
  • Capture Group

Debugging

  • Debugging Overview

Object Oriented Programming

  • Object Oriented Programming Overview
  • Mixin
  • Closure
  • IIFE

Functional Programming

  • Functional Programming

Basic Algorithm Script

  • Reverse a String
  • Factorialize a Number
  • Find the Longest Word in a String
  • Return Largest Numbers In Arrays
  • Repeat a String
  • Truncate a String
  • Finders Keepers
  • Boo Who
  • Title Case a Sentence
  • Slice and Splice
  • Falsy Bouncer
  • javascript-algorithms-anddata-structures/basic-algorithm-scripting/where-do-i-belong
  • Mutations
  • Chunky Monkey

Intermediate Algorithm Script

  • Sum All Numbers in a Range
  • Diff Two Arrays
  • Seek and Destroy
  • Wherefor Art Thou
  • Spinal Tap Case
  • Search and Replace
  • DNA Pairing
  • Missing letters
  • Sorted Union
  • Convert HTML Entities
  • Sum All Odd Fibonacci Numbers
  • Smallest Common Multiple
  • Drop it
  • Steamroller
  • Binary Agents
  • Everything Be True
  • Arguments Optional
  • Make a Person
  • Map the Debris

Wherefor Art Thou

个人思路:

  • 利用 filter 筛选数据
  • 利用 for...in 循环获取第二个参数的每个属性,并且与第一个参数中的对象进行比较
function whatIsInAName(collection, source) {
  // What's in a name?
  var arr = [];
  // Only change code below this line
  arr = collection.filter(collectionElement => {
    for (let key in source) {
      if (source[key] !== collectionElement[key]) {
        return false
      }
    }
    return true
  });

  // Only change code above this line
  return arr;
}

whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });

freeCodeCamp 提供的中级思路中使用了 Object.key() 获取第二个参数的 keys,并且利用 every() 来决定 filter() 返回值。另外一个比较有意思的地方在于在 every() 的回调函数中。

function whatIsInAName(collection, source) {
  var srcKeys = Object.keys(source);

  return collection.filter(function (obj) {
    return srcKeys.every(function (key) {
      return obj.hasOwnProperty(key) && obj[key] === source[key];
    });
  });
}

whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });

freeCodeCamp 高级思路中也提供了很有意思的解答,利用 map 获取对应的属性是否相同的 boolean 值,并通过 reducer 来求是否 map 中所有的元素都为 true。

function whatIsInAName(collection, source) {
  var srcKeys = Object.keys(source);

  return collection.filter(function (obj) {
    return srcKeys
      .map(function(key) {
        return obj.hasOwnProperty(key) && obj[key] === source[key];
      })
      .reduce(function(a, b) {
        return a && b;
      });
  });
}

// test here
whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });
← Seek and DestroySpinal Tap Case →
Copyright © 2019 罗惠东