1

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

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;

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


yint
388 声望21 粉丝

转AIing