如何使用javascript / jquery从数组中过滤数据?

时间:2018-12-28 05:23:29

标签: javascript

我已将数组声明为var user_profile = [];,并且我已经在包含以下内容的数组中推送了一些数据:

[  
   {  
      "assessmentType":"FINAL",
      "assessCatId":-1,
      "assessReason":"2",
      "assessAmount":"",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":-1,
      "assessReason":"2",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":-1,
      "assessReason":"",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":null,
      "assessAmount":"",
      "assessPenalty":"",
      "assessTotal":0
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":null,
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":"",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":"",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":"2075-09-09",
      "assessAmount":"2075-09-17",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":"2075-09-09",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":"2075-09-17",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":1,
      "assessReason":"",
      "assessAmount":"0",
      "assessPenalty":"0",
      "assessTotal":0
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":2,
      "assessReason":"",
      "assessAmount":"0",
      "assessPenalty":"0",
      "assessTotal":0
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":3,
      "assessReason":"",
      "assessAmount":"0",
      "assessPenalty":"0",
      "assessTotal":0
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":1,
      "assessReason":"A",
      "assessAmount":"1",
      "assessPenalty":"2",
      "assessTotal":3
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":2,
      "assessReason":"B",
      "assessAmount":"3",
      "assessPenalty":"4",
      "assessTotal":7
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":3,
      "assessReason":"C",
      "assessAmount":"5",
      "assessPenalty":"6",
      "assessTotal":11
   }
]

我只需要 assessCatId = 1,2,3并且assessReason不为空的数组中的数据。所以我尝试通过过滤它:

const output = user_profile.filter(({ assessCatId,assessReason })  => assessCatId != 0 || assessReason != 'empty');
            console.log("here the outpuit is");
            console.log(output)  ;

但是它不是在过滤数组。仍然有每个数据都在数组中。我如何管理这个数组来过滤数据?

10 个答案:

答案 0 :(得分:1)

您必须使用for (var i in books) { //Start JS Code var html = '<div class="row"><div class="book_history">'; // JS Output <?php // Start PHP $catids = array(39, 37, 2); // Category for test foreach ( $catids as $catid ) { $my_query = new WP_Query( array( 'cat' => $catid, 'ignore_sticky_posts' => 1, 'posts_per_page' => 1, 'no_found_rows' => true ) ); if ( $my_query->have_posts() ) : while ( $my_query->have_posts() ) : $my_query->the_post(); ?> html += '<a href="<?php the_permalink(); ?>">»Chapter <?php the_title(); ?></a></div>'; // Javascript Code, Displaying the result of foreach with js <?php endwhile; endif; wp_reset_postdata(); } ?> // End PHP $bookcontainer.append(html); // Insert all result of foreach and js into some element } // End JS Code 而不是&&。试试

||

(assessCatId >= 1 && assessCatId <= 3) && (assessReason != '' && assessReason != null)

您还可以像这样缩短条件

var user_profile =[  
   {  
      "assessmentType":"FINAL",
      "assessCatId":-1,
      "assessReason":"2",
      "assessAmount":"",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":-1,
      "assessReason":"2",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":-1,
      "assessReason":"",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":null,
      "assessAmount":"",
      "assessPenalty":"",
      "assessTotal":0
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":null,
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":"",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":"",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":"2075-09-09",
      "assessAmount":"2075-09-17",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":"2075-09-09",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":"2075-09-17",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":1,
      "assessReason":"",
      "assessAmount":"0",
      "assessPenalty":"0",
      "assessTotal":0
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":2,
      "assessReason":"",
      "assessAmount":"0",
      "assessPenalty":"0",
      "assessTotal":0
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":3,
      "assessReason":"",
      "assessAmount":"0",
      "assessPenalty":"0",
      "assessTotal":0
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":1,
      "assessReason":"A",
      "assessAmount":"1",
      "assessPenalty":"2",
      "assessTotal":3
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":2,
      "assessReason":"B",
      "assessAmount":"3",
      "assessPenalty":"4",
      "assessTotal":7
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":3,
      "assessReason":"C",
      "assessAmount":"5",
      "assessPenalty":"6",
      "assessTotal":11
   }
]

const output = user_profile.filter(({ assessCatId,assessReason })  => (assessCatId >= 1 && assessCatId <= 3) && (assessReason != '' && assessReason != null));
console.log("here the outpuit is");
console.log(output)  ;

(assessCatId >= 1 && assessCatId <= 3) && assessReason)

答案 1 :(得分:1)

您可以在数组中插入任何值并检查它

let k=[  
   {  
      "assessmentType":"FINAL",
      "assessCatId":-1,
      "assessReason":"2",
      "assessAmount":"",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":-1,
      "assessReason":"2",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":-1,
      "assessReason":"",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":null,
      "assessAmount":"",
      "assessPenalty":"",
      "assessTotal":0
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":null,
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":"",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":"",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":"2075-09-09",
      "assessAmount":"2075-09-17",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":"2075-09-09",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":"2075-09-17",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":1,
      "assessReason":"",
      "assessAmount":"0",
      "assessPenalty":"0",
      "assessTotal":0
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":2,
      "assessReason":"",
      "assessAmount":"0",
      "assessPenalty":"0",
      "assessTotal":0
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":3,
      "assessReason":"",
      "assessAmount":"0",
      "assessPenalty":"0",
      "assessTotal":0
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":1,
      "assessReason":"A",
      "assessAmount":"1",
      "assessPenalty":"2",
      "assessTotal":3
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":2,
      "assessReason":"B",
      "assessAmount":"3",
      "assessPenalty":"4",
      "assessTotal":7
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":3,
      "assessReason":"C",
      "assessAmount":"5",
      "assessPenalty":"6",
      "assessTotal":11
   }
]

let arr=[1,2,3]
let d=k.filter(a=>arr.indexOf(a.assessCatId) > -1 && a.assessReason!="" && a.assessReason!=null)
console.log(d)

答案 2 :(得分:0)

您可以进行sub textEffectiveInterestRate_onchange() Me.textNominalInterstRate.value = calculateNIR(Me.textEffectiveInterestRate.value, Me.textForceOfInterest.value, etc...) Me.textForceOfInterest.value = calculateForceOfInterest(Me.textEffectiveInterestRate.value, Me.textNominalInterstRate.value, etc...) ' And every other function aside from calculateEIR() end sub

代码:

assessCatId > 0 && assessReason

答案 3 :(得分:0)

尝试一下:

const user_profile = [{"assessmentType": "FINAL","assessCatId": -1,"assessReason": "2","assessAmount": "","assessTotal": null},{"assessmentType": "FINAL","assessCatId": -1,"assessReason": "2","assessTotal": null},{"assessmentType": "FINAL","assessCatId": -1,"assessReason": "","assessTotal": null},{"assessmentType": "FINAL","assessCatId": 0,"assessReason": null,"assessAmount": "","assessPenalty": "","assessTotal": 0},{"assessmentType": "FINAL","assessCatId": 0,"assessReason": null,"assessTotal": null},{"assessmentType": "FINAL","assessCatId": 0,"assessReason": "","assessTotal": null},{"assessmentType": "FINAL","assessCatId": 0,"assessReason": "","assessTotal": null},{"assessmentType": "FINAL","assessCatId": 0,"assessReason": "2075-09-09","assessAmount": "2075-09-17","assessTotal": null},{"assessmentType": "FINAL","assessCatId": 0,"assessReason": "2075-09-09","assessTotal": null},{"assessmentType": "FINAL","assessCatId": 0,"assessReason": "2075-09-17","assessTotal": null},{"assessmentType": "FINAL","assessCatId": 1,"assessReason": "","assessAmount": "0","assessPenalty": "0","assessTotal": 0},{"assessmentType": "FINAL","assessCatId": 2,"assessReason": "","assessAmount": "0","assessPenalty": "0","assessTotal": 0},{"assessmentType": "FINAL","assessCatId": 3,"assessReason": "","assessAmount": "0","assessPenalty": "0","assessTotal": 0},{"assessmentType": "FINAL","assessCatId": 1,"assessReason": "A","assessAmount": "1","assessPenalty": "2","assessTotal": 3},{"assessmentType": "FINAL","assessCatId": 2,"assessReason": "B","assessAmount": "3","assessPenalty": "4","assessTotal": 7},{"assessmentType": "FINAL","assessCatId": 3,"assessReason": "C","assessAmount": "5","assessPenalty": "6","assessTotal": 11}];
const output = user_profile.filter(({assessCatId, assessReason}) => assessCatId > 0 && assessReason);
console.log(output);

var res = arr.filter( el => el.assessCatId > 0 && el.assessCatId < 4 && el.assessReason  ) ;

答案 4 :(得分:0)

var arr = [{ "assessmentType": "FINAL", "assessCatId": -1, "assessReason": "2", "assessAmount": "", "assessTotal": null }, { "assessmentType": "FINAL", "assessCatId": -1, "assessReason": "2", "assessTotal": null }, { "assessmentType": "FINAL", "assessCatId": -1, "assessReason": "", "assessTotal": null }, { "assessmentType": "FINAL", "assessCatId": 0, "assessReason": null, "assessAmount": "", "assessPenalty": "", "assessTotal": 0 }, { "assessmentType": "FINAL", "assessCatId": 0, "assessReason": null, "assessTotal": null }, { "assessmentType": "FINAL", "assessCatId": 0, "assessReason": "", "assessTotal": null }, { "assessmentType": "FINAL", "assessCatId": 0, "assessReason": "", "assessTotal": null }, { "assessmentType": "FINAL", "assessCatId": 0, "assessReason": "2075-09-09", "assessAmount": "2075-09-17", "assessTotal": null }, { "assessmentType": "FINAL", "assessCatId": 0, "assessReason": "2075-09-09", "assessTotal": null }, { "assessmentType": "FINAL", "assessCatId": 0, "assessReason": "2075-09-17", "assessTotal": null }, { "assessmentType": "FINAL", "assessCatId": 1, "assessReason": "", "assessAmount": "0", "assessPenalty": "0", "assessTotal": 0 }, { "assessmentType": "FINAL", "assessCatId": 2, "assessReason": "", "assessAmount": "0", "assessPenalty": "0", "assessTotal": 0 }, { "assessmentType": "FINAL", "assessCatId": 3, "assessReason": "", "assessAmount": "0", "assessPenalty": "0", "assessTotal": 0 }, { "assessmentType": "FINAL", "assessCatId": 1, "assessReason": "A", "assessAmount": "1", "assessPenalty": "2", "assessTotal": 3 }, { "assessmentType": "FINAL", "assessCatId": 2, "assessReason": "B", "assessAmount": "3", "assessPenalty": "4", "assessTotal": 7 }, { "assessmentType": "FINAL", "assessCatId": 3, "assessReason": "C", "assessAmount": "5", "assessPenalty": "6", "assessTotal": 11 }]
var o = arr.filter((f) => [1, 2, 3].indexOf(f.assessCatId) != -1 && f.assessReason)
console.log(o)

答案 5 :(得分:0)

只需替换为({ assessCatId,assessReason })=>assessCatId>0 && assessReason!='')

var user_profile = [  
   {  
      "assessmentType":"FINAL",
      "assessCatId":-1,
      "assessReason":"2",
      "assessAmount":"",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":-1,
      "assessReason":"2",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":-1,
      "assessReason":"",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":null,
      "assessAmount":"",
      "assessPenalty":"",
      "assessTotal":0
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":null,
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":"",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":"",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":"2075-09-09",
      "assessAmount":"2075-09-17",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":"2075-09-09",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":"2075-09-17",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":1,
      "assessReason":"",
      "assessAmount":"0",
      "assessPenalty":"0",
      "assessTotal":0
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":2,
      "assessReason":"",
      "assessAmount":"0",
      "assessPenalty":"0",
      "assessTotal":0
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":3,
      "assessReason":"",
      "assessAmount":"0",
      "assessPenalty":"0",
      "assessTotal":0
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":1,
      "assessReason":"A",
      "assessAmount":"1",
      "assessPenalty":"2",
      "assessTotal":3
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":2,
      "assessReason":"B",
      "assessAmount":"3",
      "assessPenalty":"4",
      "assessTotal":7
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":3,
      "assessReason":"C",
      "assessAmount":"5",
      "assessPenalty":"6",
      "assessTotal":11
   }
];


const output = user_profile.filter(({ assessCatId,assessReason })=>assessCatId>0 && assessReason!='');
  
  console.log("output", output.length, output);

答案 6 :(得分:0)

var output = [];
for(var i=0; i < user_profile.length; i++){
    var assessCatId = user_profile[i].assessCatId;
    var assessReason = user_profile[i].assessReason;
    if((assessCatId >= 1 && assessCatId <= 3) && (assessReason != '' && assessReason != null)){
        output.push(user_profile[i]);
    }
}
console.log(output);

答案 7 :(得分:0)

  

我认为您应该先尝试基本的id,然后再潜水   到数组帮助器/原型函数。这将帮助您   了解发生了什么事。

for loop

答案 8 :(得分:0)

使用过滤器功能并使用第二个this参数来传递CatIds数组。测试当前元素的ID是否包含在CatId中,并使用已定义的非空字符串为true,否则为false的事实。

const arr = [  
   {  
      "assessmentType":"FINAL",
      "assessCatId":-1,
      "assessReason":"2",
      "assessAmount":"",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":-1,
      "assessReason":"2",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":-1,
      "assessReason":"",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":null,
      "assessAmount":"",
      "assessPenalty":"",
      "assessTotal":0
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":null,
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":"",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":"",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":"2075-09-09",
      "assessAmount":"2075-09-17",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":"2075-09-09",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":0,
      "assessReason":"2075-09-17",
      "assessTotal":null
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":1,
      "assessReason":"",
      "assessAmount":"0",
      "assessPenalty":"0",
      "assessTotal":0
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":2,
      "assessReason":"",
      "assessAmount":"0",
      "assessPenalty":"0",
      "assessTotal":0
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":3,
      "assessReason":"",
      "assessAmount":"0",
      "assessPenalty":"0",
      "assessTotal":0
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":1,
      "assessReason":"A",
      "assessAmount":"1",
      "assessPenalty":"2",
      "assessTotal":3
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":2,
      "assessReason":"B",
      "assessAmount":"3",
      "assessPenalty":"4",
      "assessTotal":7
   },
   {  
      "assessmentType":"FINAL",
      "assessCatId":3,
      "assessReason":"C",
      "assessAmount":"5",
      "assessPenalty":"6",
      "assessTotal":11
   }
];

const catIds = [1, 2, 3];

// return elements with acceptable catId (id matches at least one) and truthy reason (non-empty, non-null, non-undefined)
function filterFn({ assessCatId: id, assessReason: reason }) {
  return this.includes(id) && !!reason
};

const res = arr.filter(filterFn, catIds);

console.log(res);

答案 9 :(得分:0)

使用&&代替||因为在&&的情况下,当两个条件都为true时返回true,而在||的情况下则返回true如果任一条件为true,则返回true。 由于在您的情况下,您都希望同时需要assetCatId!= 0和assessmentReason =不为空, 因此,对于&&将起作用。

代码段

(assessCatId> = 1 && AssessmentCatId <= 3)&&(assessReason!=''&& AssessmentReason!= null)