map every forEach diff javascript - whatIsInAName

https://stackoverflow.com/que...數組

The difference is in the return values.app

.map()

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

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

.filter()

filter方法的參數是一個函數,全部數組成員依次執行該函數,返回結果爲true的成員組成一個新數組返回.ui


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

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 }]spa

example

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;code

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

本身寫的代碼(扶額)

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

相關文章
相關標籤/搜索