中级算法 - 找出包含特定键值对的对象

告诉我们发生了什么:
咱就是说有没有更优雅的方式呢?感觉自己是个麻瓜。

**你目前的代码**
function whatIsInAName(collection, source) {
//const arr = [];
// 只修改这一行下面的代码
const result = collection
//过滤出source属性名是collection属性名的子集
.filter((collecItem) => {
  let collecItemKey = Object.keys(collecItem)
  let sourceKey = Object.keys(source)
  return sourceKey.every((itemKey) => {
    return collecItemKey.includes(itemKey)
  })
})
//过滤出source属性值为collection属性值的子集
.filter((collecItem) => {
  let collecItemValue = Object.values(collecItem)
  let sourceValue = Object.values(source)
  return sourceValue.every((value) => {
    return collecItemValue.includes(value)
  })
})
// 只修改这一行上面的代码
return result
}

挑战: 中级算法 - 找出包含特定键值对的对象

挑战的链接:

function whatIsInAName(collection, source) {
  const arr = [];
  // 只修改这一行下面的代码
  collection.forEach(element => {
    let flag = true
    for (const i in source) {
      if (source[i] !== element[i]) {
        flag = false
       break
      }
    }
    if (flag) arr.push(element)
  })

  // 只修改这一行上面的代码
  return arr;
}

函数式编程没学明白

秒啊,头脑清晰

————凑字数—————