簡體   English   中英

Javascript 計數出現次數

[英]Javascript count number of occurrence

我有以下數組數據,我正在計算使用包含條件匹配的記錄數。

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

輸出

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

我得到了想要的結果,有沒有更有效的方法來編寫上面的最小代碼?

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

使用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)

無需使用String.join Array 也有函數Array.prototype.includes來檢查項目是否存在於數組中。

使用它,您可以找到滿足您條件的 subArray。 並且使用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);

或者簡單地說,您可以使用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, });

使用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);

在您的解決方案中,您在數組中迭代兩次,這是不必要的,好像一個對象是您正在搜索的兩個對象之一,然后它不是另一個,因此您可以在一個循環中完成所有操作。

 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