Javascript check how many times an item appears in a list and then add the lists together

Viewed 71

I am trying to get check how many times a fixture has a post made about it, I have a list of all the teams and then all the teams which have fixtures. Here is a list of the fixtures.

(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"]

And then here's the one with posts made about them

0: ["Liverpool v Leeds United"]
1: ["Crystal Palace v Southampton"]
2: ["Crystal Palace v Southampton"]

So I basically want to make a final array like this:

 (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]

I am using this code to make a google bar chart. Here is the code that I have created so far.

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

The fixposts array is all of the teams that have fixtures that week and the posts is the teams which have a fixture that week and a post made about them. How would I then combine both of these so I can have a single array with all fixtures and either 0 or how many posts were made about them? Thank you.

1 Answers

You can use for-loop to iterate through your fixposts & posts then check if the value match if yes increase count value and store in new array .

Demo code :

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

Related