Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

I got this format in json. I need unique values of "volume". So in result I'd get only '30' and '40'. I used loop inside of another loop, but it returns unique values from each product, which is not what I want. I'm using Angular 1 in Ionic.

{

"data": [
    {
        "id": 1,
        "title": "Category title",
        "products": [
            {
                "id": 1,
                "title": "product title",
                "volume": "40"
            },
            {
                "id": 2,
                "title": "product title",
                "volume": "30"
            }
        ]
    },

    {
        "id": 2,
        "title": "Another Category title",
        "products": [
            {
                "id": 3,
                "title": "product title",
                "volume": "40"
            },
            {
                "id": 3,
                "title": "product title",
                "volume": "30"
            }
        ]
    }

]

}

Thanks

share|improve this question
up vote 2 down vote accepted

Try this it will work :

JSON :

var obj = {
    "data": [{
            "id": 1,
            "title": "Category title",
            "products": [{
                "id": 1,
                "title": "product title",
                "volume": "40"
            }, {
                "id": 2,
                "title": "product title",
                "volume": "30"
            }]
        },

        {
            "id": 2,
            "title": "Another Category title",
            "products": [{
                "id": 3,
                "title": "product title",
                "volume": "40"
            }, {
                "id": 3,
                "title": "product title",
                "volume": "30"
            }]
        }

    ]

};

Logic implementation :

var arr = [];
for (var item in obj.data) {
  for (var innData in obj.data[item].products) {
   var volume =  obj.data[item].products[innData].volume;
   if(arr.indexOf(volume) == '-1') {
     arr.push(volume);
   }
  }
}

console.log(arr);

Output :

enter image description here

Working fiddle : https://jsfiddle.net/bo2drv5x/

share|improve this answer
    
Thanks a lot, works as expected! – Roland Jun 9 at 7:43

To get the unique values

var uniqueVolumes = $.unique($.map(json.data, function(datum) { 
        return datum.products.map(function(product) { 
                                  return product.volume;
         });
    });
  );
share|improve this answer

Using core java script you can find like this:

var jsonData = {"data": [
    {
        "id": 1,
        "title": "Category title",
        "products": [
            {
                "id": 1,
                "title": "product title",
                "volume": "40"
            },
            {
                "id": 2,
                "title": "product title",
                "volume": "30"
            }
        ]
    },

    {
        "id": 2,
        "title": "Another Category title",
        "products": [
            {
                "id": 3,
                "title": "product title",
                "volume": "40"
            },
            {
                "id": 3,
                "title": "product title",
                "volume": "30"
            }
        ]
    }

]

};
var uniqueArr = [];
var jsonData = jsonData["data"];
var fullObjectLength = Object.keys(jsonData).length
for(var i=0; i<fullObjectLength;i++)
{
	//console.log(jsonData[i]["products"]);
  var jsonProductsLength = jsonData[i]["products"].length;
  for(var j=0; j<jsonProductsLength; j++)
  {
  	 var volumeKeyValue = jsonData[i]["products"][j]["volume"];   
    var itemTobePushedInArray = uniqueArr.indexOf(volumeKeyValue) == -1;
    if(itemTobePushedInArray)
    {
     uniqueArr.push(volumeKeyValue);
     console.log(uniqueArr);
    }
  }
}

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.