【问题标题】:Trying to rewrite _.where function in Underscore library试图重写下划线库中的 _.where 函数
【发布时间】:2014-12-06 18:49:22
【问题描述】:

我正在尝试重写执行此操作的 _.where():

where_.where(列表,属性) 遍历列表中的每个值,返回包含属性中列出的所有键值对的所有值的数组。

我无法访问作为属性传递给 where() 的数组中的不同对象。如何访问数组中对象的值并对其进行迭代?

这就是我所拥有的:

 var arr = [{title: "Cymbeline", author: "Shakespeare", year: 1611},
               {title: "The Tempest", author: "Shakespeare", year: 1611},
               {title: "Cymbeline", author: "steve", year: 1671}];
    var newArr = [];
    var where = function (list, properties) {
        for (var i = 0; i < list.length; i++){
            console.log(list[i]); // just gives the array again??
            if (list[i] == properties) {// this conditional should compare the values in arr and     properties
                newArr.push(list[i]); // if the values in arr match those in properties then the        respective objects get pushed to newArr
        }
        }
        return newArr;
    }


console.log(where(arr, {author: "Shakespeare", year: 1611}));

当最后返回 newArr 时,我最终得到的是 arr[] 和一个空的“[]”。

【问题讨论】:

  • 首先,在函数内部而不是外部定义newArr。其次,注意你比较对象,这么简单的===是行不通的。
  • 好的 -- 将 newArr 添加到 fxn 但我不知道更改范围将如何产生影响?我如何访问对象本身的值?
  • 要遍历对象的属性,请使用for...inhasOwnProperty 来检查它是对象的属性,而不是原型链下游的对象。

标签: javascript arrays object functional-programming underscore.js


【解决方案1】:

OP 想要的是过滤具有可配置(不断变化的)条件的列表,但这些配置如何影响过滤过程的固定思维方式(算法)。

明确这一点,可以直接实现一个功能,实现这一点...从提供的propertyMap 构建过滤条件,并在过滤提供的列表时使用它。

不知道应该如何实现where ... 作为whereEverywhereSome,上述主要方法在提供两种变体方面非常灵活,它们仅在Array.every 和@987654326 的用法上有所不同@...

var list = [{
  title: "Cymbeline",
  author: "Shakespeare",
  year: 1611
}, {
  title: "The Tempest",
  author: "Shakespeare",
  year: 1611
}, {
  title: "Cymbeline",
  author: "steve",
  year: 1671
}];


var where = function (list, propertMap) {
  var
    keyList   = Object.keys(propertMap),

    condition = function (item) {             // where every.
      return keyList.every(function (key) {
        var
          property = item[key]
        ;
        return (!!property && (property === propertMap[key]));
      });
    }
  ;
  return list.filter(condition);
};

console.log(where(list, {author: "Shakespeare", year: 1611}));
console.log(where(list, {author: "Shakespeare"}));
console.log(where(list, {author: "Shakespeare", title: "Cymbeline"}));
console.log(where(list, {title: "Cymbeline"}));


var whereSome = function (list, propertMap) {
  var
    keyList   = Object.keys(propertMap),

    condition = function (item) {             // where some.
      return keyList.some(function (key) {
        var
          property = item[key]
        ;
        return (!!property && (property === propertMap[key]));
      });
    }
  ;
  return list.filter(condition);
};

console.log(where(list, {author: "steve", year: 1611}));
console.log(whereSome(list, {author: "steve", year: 1611}));

【讨论】:

    猜你喜欢
    • 2010-10-21
    • 2010-12-21
    • 2015-01-07
    • 1970-01-01
    • 2022-12-08
    • 1970-01-01
    • 2012-08-10
    • 2015-07-21
    • 2013-08-28
    相关资源
    最近更新 更多