只要对象属性不是NaN,就将对象推送到数组

时间:2013-03-05 17:36:18

标签: javascript arrays object

我知道我可以使用.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 个答案:

答案 0 :(得分: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语句的一部分进行检查

答案 1 :(得分:1)

只需从

更改测试行
if(itemPath[i].attributes.id==id)

在您要检查的媒体资源上使用isNaN function

var attr = itemPath[i].attributes;
if (attr.id==id && !isNaN(attr.title) && !isNaN(attr.categoryid) && !isNaN(attr.possible))

答案 2 :(得分: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
               });
            }
         });
      }
   }
}

答案 3 :(得分: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
               });
         });
      }
   }
}

看起来你没有正确使用地图。映射就像列表压缩一样,它迭代序列以对每个元素执行某种操作并返回一个新序列

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

仅供参考,这是过滤器的工作原理。 Filter接受谓词函数(aka,Boolean函数)来构建新序列。如果谓词返回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

看起来你不需要物品数组,甚至不需要将新元素推到它上面。

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
               });
         });
      }
   }
}

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