Javascript 检查某个项目在列表中出现的次数,然后将列表相加
Javascript check how many times an item appears in a list and then add the lists together
我想检查一个固定装置有多少次post,我有一个所有球队的列表,然后是所有有固定装置的球队。
这是固定装置的列表。
(8) [Array(1), Array(1), Array(1), Array(1), Array(1), Array(1), Array(1), Array(1)]
0: ["Fulham v Arsenal"]
1: ["Crystal Palace v Southampton"]
2: ["Liverpool v Leeds United"]
3: ["West Ham United v Newcastle United"]
4: ["West Bromwich Albion v Leicester City"]
5: ["Tottenham v Everton"]
6: ["Sheffield United v Wolverhampton Wanderers"]
7: ["Brighton and Hove Albion v Chelsea"]
然后是post关于他们的
0: ["Liverpool v Leeds United"]
1: ["Crystal Palace v Southampton"]
2: ["Crystal Palace v Southampton"]
所以我基本上想像这样制作一个最终数组:
(8) [Array(1), Array(1), Array(1), Array(1), Array(1), Array(1), Array(1), Array(1)]
0: ["Fulham v Arsenal", 0]
1: ["Crystal Palace v Southampton", 2]
2: ["Liverpool v Leeds United", 1]
3: ["West Ham United v Newcastle United", 0]
4: ["West Bromwich Albion v Leicester City", 0]
5: ["Tottenham v Everton", 0]
6: ["Sheffield United v Wolverhampton Wanderers", 0]
7: ["Brighton and Hove Albion v Chelsea", 0]
我正在使用此代码制作 google 条形图。这是我到目前为止创建的代码。
var fixposts = [];
var posts = [];
var postsc = [];
var finala = [];
var y = 0;
var z = 0;
function getOccurrence(array, value) {
var count = 0;
array.forEach((v) => (v === value && count++));
return count;
}
<c:forEach items="${fixtures}" var="fix" varStatus="count">
fixposts.push(['<c:out value="${fix.home.teamName} v ${fix.away.teamName}"/>']);
</c:forEach>
console.log(fixposts);
<c:forEach items="${posts}" var="post" varStatus="count">
posts.push(['<c:out value="${post.fixture.home.teamName} v ${post.fixture.away.teamName}"/>']);
</c:forEach>
console.log(posts);
fixposts 数组是那周有比赛的所有球队,posts 是那周有比赛的球队,post 大约他们。然后我如何将这两者结合起来,这样我就可以拥有一个包含所有灯具的数组,以及 0 或多少 posts 是关于它们的?
谢谢。
您可以使用 for-loop
遍历 fixposts
和 posts
然后检查值是否匹配,如果匹配则增加 count
值并存储在新数组中。
演示代码 :
//this you will get from jsp code ..
var fixposts = [
["Fulham v Arsenal"],
["Crystal Palace v Southampton"],
["Liverpool v Leeds United"],
["West Ham United v Newcastle United"],
["West Bromwich Albion v Leicester City"],
["Tottenham v Everton"],
["Sheffield United v Wolverhampton Wanderers"],
["Brighton and Hove Albion v Chelsea"]
]
var posts = [
["Liverpool v Leeds United"],
["Crystal Palace v Southampton"],
["Crystal Palace v Southampton"]
]
var new_array = [];
//loop through fixpost array
for (var i = 0; i < fixposts.length; i++) {
var count = 0;
//use `0` because fixpost is array of arary so inner array is `0` index
for (var j = 0; j < posts.length; j++) {
//comapre
if (fixposts[i][0] == posts[j][0]) {
count++;
}
}
//store in new array
new_array.push([fixposts[i][0], count])
}
console.log(new_array)
我想检查一个固定装置有多少次post,我有一个所有球队的列表,然后是所有有固定装置的球队。 这是固定装置的列表。
(8) [Array(1), Array(1), Array(1), Array(1), Array(1), Array(1), Array(1), Array(1)]
0: ["Fulham v Arsenal"]
1: ["Crystal Palace v Southampton"]
2: ["Liverpool v Leeds United"]
3: ["West Ham United v Newcastle United"]
4: ["West Bromwich Albion v Leicester City"]
5: ["Tottenham v Everton"]
6: ["Sheffield United v Wolverhampton Wanderers"]
7: ["Brighton and Hove Albion v Chelsea"]
然后是post关于他们的
0: ["Liverpool v Leeds United"]
1: ["Crystal Palace v Southampton"]
2: ["Crystal Palace v Southampton"]
所以我基本上想像这样制作一个最终数组:
(8) [Array(1), Array(1), Array(1), Array(1), Array(1), Array(1), Array(1), Array(1)]
0: ["Fulham v Arsenal", 0]
1: ["Crystal Palace v Southampton", 2]
2: ["Liverpool v Leeds United", 1]
3: ["West Ham United v Newcastle United", 0]
4: ["West Bromwich Albion v Leicester City", 0]
5: ["Tottenham v Everton", 0]
6: ["Sheffield United v Wolverhampton Wanderers", 0]
7: ["Brighton and Hove Albion v Chelsea", 0]
我正在使用此代码制作 google 条形图。这是我到目前为止创建的代码。
var fixposts = [];
var posts = [];
var postsc = [];
var finala = [];
var y = 0;
var z = 0;
function getOccurrence(array, value) {
var count = 0;
array.forEach((v) => (v === value && count++));
return count;
}
<c:forEach items="${fixtures}" var="fix" varStatus="count">
fixposts.push(['<c:out value="${fix.home.teamName} v ${fix.away.teamName}"/>']);
</c:forEach>
console.log(fixposts);
<c:forEach items="${posts}" var="post" varStatus="count">
posts.push(['<c:out value="${post.fixture.home.teamName} v ${post.fixture.away.teamName}"/>']);
</c:forEach>
console.log(posts);
fixposts 数组是那周有比赛的所有球队,posts 是那周有比赛的球队,post 大约他们。然后我如何将这两者结合起来,这样我就可以拥有一个包含所有灯具的数组,以及 0 或多少 posts 是关于它们的? 谢谢。
您可以使用 for-loop
遍历 fixposts
和 posts
然后检查值是否匹配,如果匹配则增加 count
值并存储在新数组中。
演示代码 :
//this you will get from jsp code ..
var fixposts = [
["Fulham v Arsenal"],
["Crystal Palace v Southampton"],
["Liverpool v Leeds United"],
["West Ham United v Newcastle United"],
["West Bromwich Albion v Leicester City"],
["Tottenham v Everton"],
["Sheffield United v Wolverhampton Wanderers"],
["Brighton and Hove Albion v Chelsea"]
]
var posts = [
["Liverpool v Leeds United"],
["Crystal Palace v Southampton"],
["Crystal Palace v Southampton"]
]
var new_array = [];
//loop through fixpost array
for (var i = 0; i < fixposts.length; i++) {
var count = 0;
//use `0` because fixpost is array of arary so inner array is `0` index
for (var j = 0; j < posts.length; j++) {
//comapre
if (fixposts[i][0] == posts[j][0]) {
count++;
}
}
//store in new array
new_array.push([fixposts[i][0], count])
}
console.log(new_array)