JSFiddle - React, Tailwind, and code Playground
JavaScript
// カウント対象の配列
var array = [
{ id:1, 'a':'a_1', 'b':'b_1' ,'c':'' },
{ id:2, 'a':'a_2', 'b':'' ,'c':'c_2' },
{ id:3, 'a':'' , 'b':'' ,'c':'c_3' },
{ id:4, 'a':'a_4', 'b':'b_4' ,'c':'' },
];
// 結果がundefinedになってしまう
console.log( 'countArray()', countArray(array) );
function countArray( array, countkeys ){
countkeys = countkeys || ['a','b','c'];
let result = {};
$.each(countkeys, function(i, countkey) {
//console.log('countArray() - countkey',countkey);
const arr = array.filter(e => e[countkey]);
//console.log('countArray() - arr', arr);
const count = arr.lentgh;
//console.log('countArray() - count', count);
result[countkey] = count;
});
return result;
}
// テストとしてループがない状態で書いたら問題なし
console.log( 'countTest()', countTest(array) );
function countTest( array ){
const arr = array.filter(e => e['a']);
//console.log('countTest() - arr', arr);
const count = arr.length;
//console.log('countTest() - count', count); // きちんと取得できている
return count;
}