3 回答

TA贡献1804条经验 获得超3个赞
假设您有一个带有数组的对象,则可以获取值并迭代数组并获取所需键的数量。
var getResponse = { taskData: [{ title: "123456", startOn: "30-4-2019-18-0", endOn: "30-4-2019-19-0" }, { title: "123456", startOn: "30-4-2019-18-0", endOn: "30-4-2019-19-0" }, { title: "qwerty", startOn: "1-5-2019-16-30", endOn: "1-5-2019-19-0" }, { title: "asdfgh", startOn: "1-5-2019-16-30", endOn: "1-5-2019-19-0" }, { title: "zxcvbn", startOn: "2-5-2019-9-0", endOn: "2-5-2019-11-0" }], otherData: [{ title: "123456", startOn: "30-4-2019-18-0", endOn: "30-4-2019-19-0" }, { title: "qwerty", startOn: "1-5-2019-16-30", endOn: "1-5-2019-19-0" }, { title: "asdfgh", startOn: "1-5-2019-16-30", endOn: "1-5-2019-19-0" }] },
key = 'startOn',
count = Object.values(getResponse).reduce(
(c, a) => (a.forEach(({ [key]: item }) => c[item] = (c[item] || 0) + 1), c),
Object.create(null)
);
console.log(count);

TA贡献1805条经验 获得超10个赞
您可以创建一个带有属性iestartOn和value的函数。forEach如果prop匹配给定值,则在数组上使用,然后将其索引推入结果对象。
const arr = [{title: "123456", startOn: "30-4-2019-18-0", endOn: "30-4-2019-19-0"}, {title: "123456", startOn: "30-4-2019-18-0", endOn: "30-4-2019-19-0"}, {title: "qwerty", startOn: "1-5-2019-16-30", endOn: "1-5-2019-19-0"}, {title: "asdfgh", startOn: "1-5-2019-16-30", endOn: "1-5-2019-19-0"}, {title: "zxcvbn", startOn: "2-5-2019-9-0", endOn: "2-5-2019-11-0"}]
function dups(arr,key,val){
let res = [];
arr.forEach((x,i) => {
if(x[key] === val) res.push(i);
})
return res;
}
console.log(dups(arr,'startOn','30-4-2019-18-0'))

TA贡献1811条经验 获得超6个赞
您可以使用array reduce并创建一个对象,其中键将为startOn,其值将为对象的数组。然后,您可以使用该键值并使用长度来获得相同的项目数startOn
let data = [{
title: "123456",
startOn: "30-4-2019-18-0",
endOn: "30-4-2019-19-0"
},
{
title: "123456",
startOn: "30-4-2019-18-0",
endOn: "30-4-2019-19-0"
},
{
title: "qwerty",
startOn: "1-5-2019-16-30",
endOn: "1-5-2019-19-0"
},
{
title: "asdfgh",
startOn: "1-5-2019-16-30",
endOn: "1-5-2019-19-0"
},
{
title: "zxcvbn",
startOn: "2-5-2019-9-0",
endOn: "2-5-2019-11-0"
}
]
let k = data.reduce(function(acc, curr) {
if (acc[curr.startOn]) {
acc[curr.startOn].push(curr)
} else {
acc[curr.startOn] = [curr]
}
return acc;
}, {});
console.log(k["30-4-2019-18-0"].length)
添加回答
举报