0

I have a JSON array as follows:

var testJSON = [
    { "AssetA": "asset_a", "AssetB": "asset_b" },
    { "AssetA": "asset_a", "AssetB": "asset_b" },
    { "AssetA": "asset_c", "AssetB": "asset_d" },
{ "AssetA": "asset_c", "AssetB": "asset_e" }];

What I'd like to do is count all the duplicates. For example, I'd like my result to be another array, where the first two elements of the row are are the repeating values, and the third element is the number of times it repeats like so:

[{asset_a, asset_b,2},
{asset_c, asset_d,1},
{asset_c, asset_e,1}]

Here is what I have so far to be able to identify the duplicates, but it keeps getting hung up and my visual studio crashes:

for (var i = 0; i < testJSON.length; i++) {
    for (var j = i + 1; j < testJSON.length;) {
        if (testJSON[i][0] == testJSON[j][0] && testJSON[i][1] == testJSON[j][1])
            // Found the same. Remove it.
            console.log("they are equal");

        else
            // No match. Go ahead.
            j++;
    }
}
2
  • 1
    You're not incrementing j in the if branch, so the inner loop never ends. Commented Sep 13, 2016 at 20:37
  • Why do you only do j++ when the elements are not equal? Commented Sep 13, 2016 at 20:37

2 Answers 2

2

You could use an object for collecting and an array for the result set

Both properties are used as key and used for lookup in the array. If not found, the a new array is build with the needed values and inserted into the result set. Then the count gets increased.

var testJSON = [{ "AssetA": "asset_a", "AssetB": "asset_b" }, { "AssetA": "asset_a", "AssetB": "asset_b" }, { "AssetA": "asset_c", "AssetB": "asset_d" }, { "AssetA": "asset_c", "AssetB": "asset_e" }],
    result = [];

testJSON.forEach(function (a) {
    var k = a.AssetA + '|' + a.AssetB;
    if (!this[k]) {
        this[k] = [a.AssetA, a.AssetB, 0];
        result.push(this[k]);
    }
    this[k][2]++;
}, Object.create(null));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

Comments

1
  1. testJSON[i][0]

I do not believe this will work like that. What you have is an ARRAY of OBJECTS.

Therefore, testJSON[i] will return an OBJECT, not an ARRAY, therefore testJSON[i][0] shouldn't return anything valuable. What you want is testJSON[i]['AssetA']

  1. Hangup

Another thing is when if (testJSON[i][0] == testJSON[j][0] && testJSON[i][1] == testJSON[j][1]) succeds on j = i+1. In this case, you will just keep iterating on the same value of j. You need to add j++ to your forloop and delete the else clause, or handle it otherwise for desired effect.

For example

for (var i = 0; i < testJSON.length; i++) {
    for (var j = i + 1; j < testJSON.length; j++) {
        if (testJSON[i]["AssetA"] == testJSON[j]["AssetA"] && 
            testJSON[i]["AssetB"] == testJSON[j]["AssetB"])
            console.log("they are equal");
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.