Here is my code:
function createMatchList(data) {
var matchArray = [];
for (var i = 0; i < initialData.length; i++) {
var listData = cleanData(initialData[i][data]);
if (matchArray.length) {
for (var a = 0; a < matchArray.length; a++) {
if (matchArray[a][data] == listData) {
matchArray[a].Count = matchArray[a].Count + 1;
} else {
// THIS CAUSES LOOP
matchArray.push({ [data]: listData, "Count": 1 });
}
}
} else {
matchArray.push({ [data]: listData, "Count": 1 });
}
}
}
Essentially, this appears to work outside of when a matching object isn't found, I have an else
to push that object to the array and it causes a loop.
I'm not sure why this would happen and I'm totally lost..
I have an initial if/else
to see if the array is empty, if it is empty, push the initial object into the array, past this point if the array isn't empty, I check the array using a for loop and the if
statement works, but my else statement causes the loop. If I were to take that matchArray.push({ [data]: listData, "Count": 1 });
out of the else statement and just console.log something, it would successfully log for each additional iteration in the first for loop for the initialData
.
And just to give an idea of what the array looks like with an object in it:
[
{
Date: "27 June 1911",
Count: 1
}
]
My goal is to search the Array for matching Dates and if there is a match, don't push to the Array but instead update the Count. The reason for this is I have a huge raw JSON file with duplicates and I'm trying to create a new data source removing duplicates but counting the occurrences.
Thanks