计算字符串数组中的子字符串

时间:2020-10-09 10:50:49

标签: javascript arrays count substring

我想计算数组中先生和小姐的使用次数。

我从其中一个开始,一直在尝试几种解决方案。

但是我无法理解如何对整个数组的计数求和,就像在我进行console.log记录时那样,它将打印其中的每一个,而不是数组中所有Mr的和?

Deployment failed. Correlation ID: 39b0173b-8a51-42c5-a796-1d3427556194. {
  "error": {
    "code": "InternalServerError",
    "message": "There was an unexpected InternalServerError.  Please try again later.  x-ms-correlation-request-id: 844e9f35-2e9c-411a-817d-9045511558cb"
  }
}

1 个答案:

答案 0 :(得分:0)

const array = [
  "Hello Mr, Hello Miss",
  "Hello Mr, Hello Miss",
  "Hello Mr, Hello Miss",
  "Hello Mr, Hello Miss"
];
console.log(array);

const count = (array.join(' ').match(/Miss|Mr/g) || []).length;
console.log(count);