简体   繁体   English

Javascript 计数出现次数

[英]Javascript count number of occurrence

I have the following array data and i am calculating the number of records that match using the includes criteria.我有以下数组数据,我正在计算使用包含条件匹配的记录数。

var final = 
[
  [
    "61131",
    "NISSAN",
    "BOLTON",
    "MAINT"
  ],
  [
    "61132",
    "NISSAN",
    "BOLTON",
    "MAINT"
  ],
  [
    "61133",
    "TOYOTA",
    "STOCKPORT",
    "STORED"
  ],
  [
    "61134",
    "TOYOTA",
    "STOCKPORT",
    "MAINT"
  ],
  [
    "61135",
    "NISSAN",
    "STOCKPORT",
    "MAINT"
  ],
  [
    "61136",
    "NISSAN",
    null,
    null
  ]
]

and the code is this one :代码是这样的:

var obj = {};
var num1 = 0;
var num2 = 0;
for (var i=0; i<final.length; i++){
    if(final[i].join(';').includes('BOLTON') && final[i].join(';').includes('MAINT') && final[i].join(';').includes('NISSAN')) {
        num1++;
    }
}

for (var i=0; i<final.length; i++){
    if(final[i].join(';').includes('STOCKPORT') && final[i].join(';').includes('STORED') && final[i].join(';').includes('TOYOTA')) {
        num2++;
    }
}


obj['BOLTON_MAINT_NISSAN'] = num1
obj['STOCKPORT_STORED_TOYOTA'] = num2

console.log(obj)

output输出

  {  "BOLTON_MAINT_NISSAN": 2, "STOCKPORT_STORED_TOYOTA": 1}

I am getting the desired result, is there a more efficient way of writing the above code that is minimal ?我得到了想要的结果,有没有更有效的方法来编写上面的最小代码?

const count = (list, keys) => {
  return keys.reduce(
    (obj, key) => ({
      ...obj,
      [key]: list.filter(sublist =>
        key.split('_').every(keyPart => sublist.includes(keyPart))
      ).length
    }),
    {}
  );
};

count(final, ['BOLTON_MAINT_NISSAN', 'STOCKPORT_STORED_TOYOTA'])

 const final = [ [ "61131", "NISSAN", "BOLTON", "MAINT" ], [ "61132", "NISSAN", "BOLTON", "MAINT" ], [ "61133", "TOYOTA", "STOCKPORT", "STORED" ], [ "61134", "TOYOTA", "STOCKPORT", "MAINT" ], [ "61135", "NISSAN", "STOCKPORT", "MAINT" ], [ "61136", "NISSAN", null, null ] ]; const count = (list, keys) => { return keys.reduce( (obj, key) => ({ ...obj, [key]: list.filter(sublist => key.split('_').every(keyPart => sublist.includes(keyPart)) ).length }), {} ); }; var bmtNsst = count(final, ['BOLTON_MAINT_NISSAN', 'STOCKPORT_STORED_TOYOTA']); console.log(bmtNsst);

Using reduce, slice to get rid of the number, filter to get rid of the nulls, and join to make it the key.使用reduce,slice 去除数字,过滤去除空值,然后join 使其成为key。

 var final = [ ["61131", "NISSAN", "BOLTON", "MAINT"], ["61132", "NISSAN", "BOLTON", "MAINT"], ["61133", "TOYOTA", "STOCKPORT", "STORED"], ["61134", "TOYOTA", "STOCKPORT", "MAINT"], ["61135", "NISSAN", "STOCKPORT", "MAINT"], ["61136", "NISSAN", null, null] ]; var grouped = final.reduce(function(obj, data) { var key = data.slice(1).filter(Boolean).join("_"); obj[key] = (obj[key] || 0) + 1; return obj; }, {}); console.log(grouped)

There is no need to use String.join .无需使用String.join Array has also got function Array.prototype.includes to check if item is existed in array or not. Array 也有函数Array.prototype.includes来检查项目是否存在于数组中。

Using that, you can find the subArray that satisfies your condition.使用它,您可以找到满足您条件的 subArray。 And using Array.prototype.filter , you can extract the sub-arrays to satisfy the conditions as follows.并且使用Array.prototype.filter ,您可以提取子数组以满足以下条件。

 const final = [ [ "61131", "NISSAN", "BOLTON", "MAINT" ], [ "61132", "NISSAN", "BOLTON", "MAINT" ], [ "61133", "TOYOTA", "STOCKPORT", "STORED" ], [ "61134", "TOYOTA", "STOCKPORT", "MAINT" ], [ "61135", "NISSAN", "STOCKPORT", "MAINT" ], [ "61136", "NISSAN", null, null ] ]; const num1Arr = final.filter((item) => item.includes('BOLTON') && item.includes('MAINT') && item.includes('NISSAN')); const num2Arr = final.filter((item) => item.includes('STOCKPORT') && item.includes('STORED') && item.includes('TOYOTA')); const output = { BOLTON_MAINT_NISSAN: num1Arr.length, STOCKPORT_STORED_TOYOTA: num2Arr.length }; console.log(output);

Or simply, you can do it using Array.prototype.reduce .或者简单地说,您可以使用Array.prototype.reduce

 const final = [ [ "61131", "NISSAN", "BOLTON", "MAINT" ], [ "61132", "NISSAN", "BOLTON", "MAINT" ], [ "61133", "TOYOTA", "STOCKPORT", "STORED" ], [ "61134", "TOYOTA", "STOCKPORT", "MAINT" ], [ "61135", "NISSAN", "STOCKPORT", "MAINT" ], [ "61136", "NISSAN", null, null ] ]; const output = final.reduce((acc, cur) => { if (cur.includes('BOLTON') && cur.includes('MAINT') && cur.includes('NISSAN')) { acc['BOLTON_MAINT_NISSAN'] ++; } if (cur.includes('STOCKPORT') && cur.includes('STORED') && cur.includes('TOYOTA')) { acc['STOCKPORT_STORED_TOYOTA'] ++; } return acc; }, { BOLTON_MAINT_NISSAN: 0, STOCKPORT_STORED_TOYOTA: 0 }); console.log(output);

 const final = [ [ "61131", "NISSAN", "BOLTON", "MAINT" ], [ "61132", "NISSAN", "BOLTON", "MAINT" ], [ "61133", "TOYOTA", "STOCKPORT", "STORED" ], [ "61134", "TOYOTA", "STOCKPORT", "MAINT" ], [ "61135", "NISSAN", "STOCKPORT", "MAINT" ], [ "61136", "NISSAN", null, null ] ]; console.log({ BOLTON_MAINT_NISSAN: final.filter((value) => { return value[1] === 'NISSAN' && value[2] === 'BOLTON' && value[3] === 'MAINT'; }).length, STOCKPORT_STORED_TOYOTA: final.filter((value) => { return value[1] === 'TOYOTA' && value[2] === 'STOCKPORT' && value[3] === 'STORED'; }).length, });

Using Array.prototype.reduce() :使用Array.prototype.reduce()

 const final = [ [ "61131", "NISSAN", "BOLTON", "MAINT" ], [ "61132", "NISSAN", "BOLTON", "MAINT" ], [ "61133", "TOYOTA", "STOCKPORT", "STORED" ], [ "61134", "TOYOTA", "STOCKPORT", "MAINT" ], [ "61135", "NISSAN", "STOCKPORT", "MAINT" ], [ "61136", "NISSAN", null, null ] ]; let occurrence = final.reduce((obj, item)=>{ var key=item[2]+"_"+item[3]+"_"+item[1]; if(!obj[key]) obj[key]=0; obj[key]++; return obj; },{}); console.log(occurrence);

In your solution, you iterate twice through the array, which is unnecessary as if an object is one of the two you're searching, then it is not the other, so you can do all in one loop.在您的解决方案中,您在数组中迭代两次,这是不必要的,好像一个对象是您正在搜索的两个对象之一,然后它不是另一个,因此您可以在一个循环中完成所有操作。

 var final = [ ["61131", "NISSAN", "BOLTON", "MAINT"], ["61132", "NISSAN", "BOLTON", "MAINT"], ["61133", "TOYOTA", "STOCKPORT", "STORED"], ["61134", "TOYOTA", "STOCKPORT", "MAINT"], ["61135", "NISSAN", "STOCKPORT", "MAINT"], ["61136", "NISSAN", null, null] ]; var obj = { BOLTON_MAINT_NISSAN: 0, STOCKPORT_STORED_TOYOTA: 0 }; final.forEach(y => { let x = y.join(';'); if(x.includes('BOLTON') && x.includes('MAINT') && x.includes('NISSAN')) obj.BOLTON_MAINT_NISSAN++; else if ( x.includes('STOCKPORT') && x.includes('STORED') && x.includes('TOYOTA') ) obj.STOCKPORT_STORED_TOYOTA++; }) console.log(obj)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM