1

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

3 Answers 3

2

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/

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

Comments

0

To get the unique values

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

Comments

0

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

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.