资讯专栏INFORMATION COLUMN

map every forEach diff javascript - whatIsInAName

jhhfft / 967人阅读

摘要:方法的参数是一个函数,所有数组成员依次执行该函数,返回结果为的成员组成一个新数组返回自己写的代码扶额

https://stackoverflow.com/que...

The difference is in the return values.

.map()

returns a new Array of objects created by taking some action on the original item.

.every()

returns a boolean - true if every element in this array satisfies the provided testing function. An important difference with .every() is that the test function may not always be called for every element in the array. Once the testing function returns false for any element, no more array elements are iterated. Therefore, the testing function should usually have no side effects.

.forEach()

returns nothing - It iterates the Array performing a given action for each item in the Array.

.filter()

filter方法的参数是一个函数,所有数组成员依次执行该函数,返回结果为true的成员组成一个新数组返回.

Example:whatIsInAName:

Write an algorithm that will take an array for the first argument and return an array with all the objects that matches all the properties and values in the Object passed as second parameter.

whatIsInAName([{ "a": 2, "b": 2 }, { "a": 1 }, { "a": 2, "b": 2, "c": 3 }], { "a": 2, "c": 3 }) should return [{ "a": 2, "b": 2, "c": 3 }]

for
  var srcKeys = Object.keys(source);
  return collection.filter(function (obj) {
    for(var i = 0; i < srcKeys.length; i++) {
      if(!obj.hasOwnProperty(srcKeys[i]) || obj[srcKeys[i]] !== source[srcKeys[i]]) {
        return false;
      }
    }
    return true;
  });
}

-filter through the array using .filter().
-Using a for loop to loop through each item in the object.
-use a if statement to check if the object in the collection doesn"t have the key and the property value doesn"t match the value in source.
-return false if the above if statement is correct. Otherwise, return true;

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

-filter through the collection using .filter().
-return a Boolean value for the .filter() method.
-reduce to Boolean value to be returned for the .every() method.

自己写的代码(扶额)

function whatIsInAName(collection, source) {
    return collection.filter(function (obj) {
        var srcKeys = Object.keys(source);
        var all = true;
        for (var i = 0; i < srcKeys.length; i++) {
            if (obj.hasOwnProperty(srcKeys[i]) && obj[srcKeys[i]] == source[srcKeys[i]]) {
            } else {
                all = false;
            }
        }
        return check;
    });
}
map
  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;
      });
  });
}

-start by filtering through collection using Array.filter().
-map through all keys and return Boolean values based on the check conditions: both the key and its corresponding value must exist within the object we are filtering through.
-reduce the mapped Boolean values to a single Boolean that indicates whether all srcKeys pass the conditions checked above.
-This single Boolean will be used to filter through the collection.

https://forum.freecodecamp.or...

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/83883.html

相关文章

  • 实现Promise的first等各种变体

    摘要:原文地址本篇文章主要是想通过中提供的几个方法,来实现诸如等各种变体方法在标准的规范中,提供了和两种,我们首先来了解下这两个方法是干嘛的,方便我们后面工作的展开。表示只获取所有的中进入完成状态的结果,被拒绝的则忽略掉。 原文地址: https://www.xiabingbao.com/po... 本篇文章主要是想通过ES6中Promise提供的几个方法,来实现诸如first、last、n...

    cnTomato 评论0 收藏0
  • Javascript 循环和迭代

    摘要:数组循环对象循环迭代在中新增了几种迭代方法。方法为数组中的每个元素执行一次函数,直到它找到一个使返回表示可转换为布尔值的值的元素。数组为数组未被修改测试数组中某些元素是否通过指定函数的测试,若有一项终止循环返回。 循环 在Javascript中数组循环使用for循环,跟其他的语言非常类似。 //数组循环 var array = [1,2,3,4,5]; for(var i = 0; i...

    olle 评论0 收藏0
  • 学习《JavaScript经典实例》之第1~3章

    摘要:与的区别如何理解和熟练运用中的及,动态改变装换为数组返回的是数组,但是本身保持不变借用别人的方法实现继承封装对象保证的指向删除或替换数组元素方法与方法的作用是不同的,方法会直接对数组进行修改。 《JavaScript经典实例》各节中的完整代码解决了常见的编程问题,并且给出了在任何浏览器中构建Web应用程序的技术。只需要将这些代码示例复制并粘贴到你自己的项目中就行了,可以快速完成工作,并...

    vvpale 评论0 收藏0
  • JS 数组循环遍历方法的对比

    摘要:循环方法方法不改变原数组方法会给原数组中的每个元素都按顺序调用一次函数。筛选出过滤出数组中符合条件的项组成新数组代码方法方法为数组中的每个元素执行一次函数,直到它找到一个使返回表示可转换为布尔值的值的元素。 showImg(https://segmentfault.com/img/bV2QTD?w=1600&h=500); 前言 JavaScript 发展至今已经发展出多种数组的循环遍...

    BlackFlagBin 评论0 收藏0
  • JavaScript数组迭代(遍历)方法

    摘要:正文和中新增的的数组迭代方法如下其中,是新增的,其余都是新增的。指数组后,返回过滤后的新数组。它的参数跟方法是一样的所有数组成员依次执行回调函数,直到找出第一个返回值为的成员,然后返回该成员。 前言 ES5和ES6中新增了不少东西,对于数组而言,新增了不少迭代方法,让我们可以抛弃for循环,更方便的写JS代码。 正文 ES5和ES6中新增的的数组迭代方法如下: forEach map...

    light 评论0 收藏0

发表评论

0条评论

jhhfft

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<