过滤阵列并获取过滤阵列的数量

时间:2019-07-11 08:01:45

标签: javascript

我想过滤以下两个数组并获取"isimplemented: 'Yes'"元素的数量:

const arr1 = [{ProjectName: "IT", Department: "Software"}]
const arr2 = [{Name: "IT", isimplemented: "Yes"}]

我尝试使用以下方法执行相同操作,但未获得所需结果。我如何用JavaScript做到

((arr1.map(data => data.ProjectName)).filter(arr1.map(data => data.ProjectName) === arr2.map(data => data.Name) && isimplemented === "Yes")).length

2 个答案:

答案 0 :(得分:0)

  1. 首先合并不同的数组以创建一个单个数组
  2. 创建一个变量,以跟踪您的计数
  3. 使用forEach遍历数组中的元素,为每个具有isImplemented属性的数组对象增加计数:“是”

Intent mapIntent = new Intent("com.here.maps.DIRECTIONS", Uri.parse(context.getString(R.string.here_map_url_prefix, locationAddress, address)));
mapIntent.addCategory(Intent.CATEGORY_DEFAULT);
context.startActivity(mapIntent);

答案 1 :(得分:0)

您可以为已实施的项目设置一个Set,然后计算已实施的项目的出现次数。

const
    arr1 = [{ ProjectName: "IT", Department: "Software" }],
    arr2 = [{ Name: "IT", isimplemented: "Yes" }],
    implemented = arr2.reduce((s, { Name, isimplemented }) => isimplemented === 'Yes' ? s.add(Name) : s, new Set),
    count = arr1.reduce((c, { ProjectName }) => c + implemented.has(ProjectName), 0);

console.log(count);