Question

I am working with JSON that is something like as follows in Javascript:

{
"data": [
{
      "id": 1,
      "name": "Data Point Zeta",
      "unit": "456456456",
},
{
      "id": 1,
      "name": "Data Point Alpha",
      "unit": "345345",
},
{
      "id": 1,
      "name": "Data Point Maverick",
      "unit": "453455",
},
{
      "id": 1,
      "name": "Data Point Delta",
      "unit": 3534545"",
},
{
      "id": 1,
      "name": "Data Point Delta",
      "unit": 3534545"",
},
{
      "id": 1,
      "name": "Data Point Bravo",
      "unit": "534553455",
},
 {
      "id": 1,
      "name": "Data Point Bravo",
      "unit": "534553455",
},
{
      "id": 1,
      "name": "Data Point Bravo",
      "unit": "4356346436",
}
]
}

I want to be able to make this JSON unique via the name value, so for example I want to be able to parse the and change the JSON so it becomes like this:

{
"data": [
{
      "id": 1,
      "name": "Data Point Zeta",
      "unit": "456456456",
},
{
      "id": 1,
      "name": "Data Point Alpha",
      "unit": "345345",
},
{
      "id": 1,
      "name": "Data Point Maverick",
      "unit": "453455",
},
{
      "id": 1,
      "name": "Data Point Delta",
      "unit": 3534545"",
},
    {
      "id": 1,
      "name": "Data Point Bravo",
      "unit": "453455",
},
]
}

So I am retrieving the first unique instance of an object based on its name. I need to do so using basic Javascript, I can't use JQuery or any other libraries. And I don't want to have to sort the array.

I have already tried following code from the following questions but with no luck:

How to make a JSON array unique

Array unique values

Get unique results from JSON array using jQuery

The closest I got was using the following code:

var results = [];
for (var i = 0; i < myJSONArr.length; i++) {
   if (myJSONArr[i + 1].name != myJSONArr[i].name) {
         results.push(myJSONArr[i]);
   }
}

But this always gives me an undefined error and if I put in code to check for the undefined error it leaves one from the list out. So for example it gets the following names:

Data Point Zeta
Data Point Alpha
Data Point Maverick
Data Point Delta

So Data Point Bravo never gets pushed to the array.

Can anyone point me in the direction of the proper way to achieve this?

Was it helpful?

Solution

Simple check hash?

var data = [
    {
          "id": 1,
          "name": "Data Point Zeta",
          "unit": "456456456"
    },
    {
          "id": 1,
          "name": "Data Point Alpha",
          "unit": "345345"
    },
    {
          "id": 1,
          "name": "Data Point Maverick",
          "unit": "453455"
    },
    {
          "id": 1,
          "name": "Data Point Delta",
          "unit": "3534545"
    },
    {
          "id": 1,
          "name": "Data Point Bravo",
          "unit": "534553455"
    },
    {
          "id": 1,
          "name": "Data Point Bravo",
          "unit": "4356346436"
    }
];

var results = []; var checks = [];
for (var i = 0; i < data.length; i++) {
    if (!checks[data[i].name]) {
        results.push(data[i]);
    }
    checks[data[i].name] = true;
}

console.log(results);

OTHER TIPS

If your data is always sorted by name like this, you can use your for loop with one addition: on the last element the if statement will throw the undefined error, because there is no next element (myJSONArr[i + 1]). Try something like this:

var results = [];
for (var i = 0; i < myJSONArr.length; i++) {
    if (i === myJSONArr.length - 1 || myJSONArr[i + 1].name != myJSONArr[i].name) {
        results.push(myJSONArr[i]);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top