0

我知道我可以使用 .filter 来实现这一点,但是我不确定如何实现它。

我在数组中有如下对象

item {
  title : g.attributes.title,
  category : g.attributes.categoryid,
  possible: g.attributes.possible
}

但是,数组中的某些项目可能具有 NaN 属性。

我需要确保只有可能属性不是 NaN 的项目才会被推入数组。

这是我完整代码的摘录:

function load(id){
   itemPath = lev1.lev2.lev3;
   items = [];
   for (var i = 0; i<itemPath.length; i++) {
      if(itemPath[i].attributes.id==id) {
         return itemPath[i].attributes.grades.models.map(function(g) {
            items.push(
               {
                  title : g.attributes.title,
                  category : g.attributes.categoryid,
                  possible: g.attributes.possible
               });
         });
      }
   }
}
4

4 回答 4

1
function load(id){
   itemPath = lev1.lev2.lev3;
   items = [];
   for (var i = 0; i<itemPath.length; i++) {
      if(itemPath[i].attributes.id==id) {
         return itemPath[i].attributes.grades.models.map(function(g) {
            if(g.attributes.possible !== g.attributes.possible){
               return;
            }


            items.push(
               {
                  title : g.attributes.title,
                  category : g.attributes.categoryid,
                  possible: g.attributes.possible
               });
         });
      }
   }
}

NaN 是 javascript 中唯一不等于自身的属性。只需遍历属性并检查它们,或者按照其他地方的建议在循环中使用内置的 NaN() 函数。

更新

由于您只担心可能的属性,因此只需使用===self 或 isNaN()将其作为 if 语句的一部分进行检查

于 2013-03-05T17:40:03.853 回答
1

只需将您的测试线从

if(itemPath[i].attributes.id==id)

在要检查的属性上使用isNaN功能

var attr = itemPath[i].attributes;
if (attr.id==id && !isNaN(attr.title) && !isNaN(attr.categoryid) && !isNaN(attr.possible))
于 2013-03-05T17:41:14.023 回答
0

您可以isNaN()在添加之前使用并测试它...

function load(id){
   itemPath = lev1.lev2.lev3;
   items = [];
   for (var i = 0; i<itemPath.length; i++) {
      if(itemPath[i].attributes.id==id) {
         return itemPath[i].attributes.grades.models.map(function(g) {
            if( isNaN(g.attributes.title) || isNaN(g.attributes.categoryid) || isNaN(g.attributes.possible)  ){
            items.push(
               {
                  title : g.attributes.title,
                  category : g.attributes.categoryid,
                  possible: g.attributes.possible
               });
            }
         });
      }
   }
}
于 2013-03-05T17:39:30.950 回答
0

你的代码有点混乱

function load(id){
   itemPath = lev1.lev2.lev3;
   items = [];
   for (var i = 0; i<itemPath.length; i++) {
      if(itemPath[i].attributes.id==id) {
         return itemPath[i].attributes.grades.models.map(function(g) {
            items.push(
               {
                  title : g.attributes.title,
                  category : g.attributes.categoryid,
                  possible: g.attributes.possible
               });
         });
      }
   }
}

看起来您没有正确使用地图。Map 的工作方式类似于列表压缩,因为它遍历一个序列以对每个元素执行某种操作并返回一个新序列

var arr = [1,2,3];
var complexMagic = arr.map( function(n) { return n + 10; } );
// complexMagic === [11,12,13]

仅供参考,这就是过滤器的工作原理。过滤器接受一个谓词函数(又名布尔函数)来构建一个新序列。如果谓词返回 true,则元素将存储在新序列中。

var arr = [1, 123, 42, 1001, 1100];
var oddNumbers = arr.filter( function(n) {
    return 1 === (n & (-n) );
} );

// oddNumbers === [1, 123, 1001] );
// Bit hacks are fun ;P

看起来您不需要 items 数组,甚至不需要将新元素推送到它上面。

function load(id){
   itemPath = lev1.lev2.lev3;
   items = [];
   for (var i = 0; i<itemPath.length; i++) {
      if(itemPath[i].attributes.id==id) {
         return itemPath[i].attributes.grades.models.map(function(g) {

            // You don't have to return anything.
            // This might be an ok place for the NaN check.

            return ({
                  title : g.attributes.title,
                  category : g.attributes.categoryid,
                  possible: g.attributes.possible
               });
         });
      }
   }
}

我很懒,没有测试我的任何代码,所以读者要小心。如果可能,也要避免使用 push 方法。将新元素附加到数组上可能是一种低效的方法。

于 2013-03-05T18:28:43.930 回答