成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專(zhuān)欄INFORMATION COLUMN

map every forEach diff javascript - whatIsInAName

jhhfft / 1134人閱讀

摘要:方法的參數(shù)是一個(gè)函數(shù),所有數(shù)組成員依次執(zhí)行該函數(shù),返回結(jié)果為的成員組成一個(gè)新數(shù)組返回自己寫(xiě)的代碼扶額

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方法的參數(shù)是一個(gè)函數(shù),所有數(shù)組成員依次執(zhí)行該函數(shù),返回結(jié)果為true的成員組成一個(gè)新數(shù)組返回.

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.

自己寫(xiě)的代碼(扶額)

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...

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/83883.html

相關(guān)文章

  • 實(shí)現(xiàn)Promise的first等各種變體

    摘要:原文地址本篇文章主要是想通過(guò)中提供的幾個(gè)方法,來(lái)實(shí)現(xiàn)諸如等各種變體方法在標(biāo)準(zhǔn)的規(guī)范中,提供了和兩種,我們首先來(lái)了解下這兩個(gè)方法是干嘛的,方便我們后面工作的展開(kāi)。表示只獲取所有的中進(jìn)入完成狀態(tài)的結(jié)果,被拒絕的則忽略掉。 原文地址: https://www.xiabingbao.com/po... 本篇文章主要是想通過(guò)ES6中Promise提供的幾個(gè)方法,來(lái)實(shí)現(xiàn)諸如first、last、n...

    cnTomato 評(píng)論0 收藏0
  • Javascript 循環(huán)和迭代

    摘要:數(shù)組循環(huán)對(duì)象循環(huán)迭代在中新增了幾種迭代方法。方法為數(shù)組中的每個(gè)元素執(zhí)行一次函數(shù),直到它找到一個(gè)使返回表示可轉(zhuǎn)換為布爾值的值的元素。數(shù)組為數(shù)組未被修改測(cè)試數(shù)組中某些元素是否通過(guò)指定函數(shù)的測(cè)試,若有一項(xiàng)終止循環(huán)返回。 循環(huán) 在Javascript中數(shù)組循環(huán)使用for循環(huán),跟其他的語(yǔ)言非常類(lèi)似。 //數(shù)組循環(huán) var array = [1,2,3,4,5]; for(var i = 0; i...

    olle 評(píng)論0 收藏0
  • 學(xué)習(xí)《JavaScript經(jīng)典實(shí)例》之第1~3章

    摘要:與的區(qū)別如何理解和熟練運(yùn)用中的及,動(dòng)態(tài)改變裝換為數(shù)組返回的是數(shù)組,但是本身保持不變借用別人的方法實(shí)現(xiàn)繼承封裝對(duì)象保證的指向刪除或替換數(shù)組元素方法與方法的作用是不同的,方法會(huì)直接對(duì)數(shù)組進(jìn)行修改。 《JavaScript經(jīng)典實(shí)例》各節(jié)中的完整代碼解決了常見(jiàn)的編程問(wèn)題,并且給出了在任何瀏覽器中構(gòu)建Web應(yīng)用程序的技術(shù)。只需要將這些代碼示例復(fù)制并粘貼到你自己的項(xiàng)目中就行了,可以快速完成工作,并...

    vvpale 評(píng)論0 收藏0
  • JS 數(shù)組循環(huán)遍歷方法的對(duì)比

    摘要:循環(huán)方法方法不改變?cè)瓟?shù)組方法會(huì)給原數(shù)組中的每個(gè)元素都按順序調(diào)用一次函數(shù)。篩選出過(guò)濾出數(shù)組中符合條件的項(xiàng)組成新數(shù)組代碼方法方法為數(shù)組中的每個(gè)元素執(zhí)行一次函數(shù),直到它找到一個(gè)使返回表示可轉(zhuǎn)換為布爾值的值的元素。 showImg(https://segmentfault.com/img/bV2QTD?w=1600&h=500); 前言 JavaScript 發(fā)展至今已經(jīng)發(fā)展出多種數(shù)組的循環(huán)遍...

    BlackFlagBin 評(píng)論0 收藏0
  • JavaScript數(shù)組迭代(遍歷)方法

    摘要:正文和中新增的的數(shù)組迭代方法如下其中,是新增的,其余都是新增的。指數(shù)組后,返回過(guò)濾后的新數(shù)組。它的參數(shù)跟方法是一樣的所有數(shù)組成員依次執(zhí)行回調(diào)函數(shù),直到找出第一個(gè)返回值為的成員,然后返回該成員。 前言 ES5和ES6中新增了不少東西,對(duì)于數(shù)組而言,新增了不少迭代方法,讓我們可以?huà)仐塮or循環(huán),更方便的寫(xiě)JS代碼。 正文 ES5和ES6中新增的的數(shù)組迭代方法如下: forEach map...

    light 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<