2

Here is my JSON code. I'm storing this json in an array.

total: {
limited: {
  things: "451",
    platforms: [
        {
           count: "358",
           id: "Windows"
        },
        {
           count: "44",
           id: "X11"
        },
        {
           count: "42",
           id: "Macintosh"
        },
        {
           count: "2",
           id: "Linux"
        },
        {
           count: "1",
           id: "iPhone"
        },
        {
           count: "1",
           id: "iPod"
        }
     ]
  },
}

When i want to show the count of things in total > limited > things, I'm using the below code and it's working fine.

document.getElementById( "limited" ).value = arr.total.limited.things;

It's showing the 'things' value in 'limited' div area.

But I want to show the count of the particular id in platforms.

total > limited > platforms > id > windows.

How can i show the value of particular id from above json?

document.getElementById( "limited" ).value = arr.total.limited.platforms[0].count;

is showing the count but, the order of platforms always change, so we don't know where the windows is in the order exactly to use the above method.

How can we show the count of particular id from above json?

Also, how can we combine particular multiple id's count? for example, how to know all the count of Macintosh, iphone & ipod count combined?

Thanks.

1
  • You can use the $.each() of jquery Commented Sep 2, 2015 at 8:33

3 Answers 3

3

You can filter the array to look for the Windows entry. Then, when you've got an array with only one element, access the count property of the the first element:

arr.total.limited.platforms.filter(
        function(el) { return el.id == "Windows"; })[0].count

Getting the sum of counts for multiple platforms could be done like this by using the Array.map function:

// here, arr is the structure you describe in your question, and platf is an
// array of all desired platforms
function combinedCount(arr, platf) {
    // for each element of the list of platforms in `arr`, we check
    // if its id is inside the list of desired platforms,
    // and return either its count or 0
    var x = arr.total.limited.platforms.map(function(el) {
            return (platf.indexOf(el.id) != -1) ? parseInt(el.count) : 0; });

    // now x is an array of counts for the relevant platforms and
    // 0 for all others, so we can just add its elements and return the sum
    var count = 0;
    for (var i = 0; i < x.length; i++) count += x[i];
    return count;
}

You'd use it like this:

combinedCount(arr, ["Windows", "Linux"])
// returns 360
Sign up to request clarification or add additional context in comments.

4 Comments

Excellent :) How can we combine multiple ids & show the total count of multiple ids?
Works perfect :) Array.map is fine. But when i needed to use multiple filter codes, if the first filter returns no value & if the id is not there in the array, it's not proceeding to the next filter and even the next id is there in the array, it's not producing the value of that array. If the first filter has no values, it should leave that and go to next filter and if it finds that id, it should display the count. How can we do this using first filter code? not Array.map
As the Array.map is returning 0 if there is no combined count, the filter code also should return 0 if there is no particular id & count. I'm going to have multiple filters so, it shoukld go through all the filters and produce all the counts. Thanks
A little problem, from the first code example above, how can we return a zero if the id windows is not found in the array?
0

you can iterate over the elements of the platforms array, an look for the one you need.

for (var p in total.element.platform) {
 if (total.element.platform[p].id == "iPhone") {
   alert(total.element.platform[p].count)
  }
}

If its possible you can structure your "platforms" as an object, that makes it possible to adress the platforms by key, this way you dont need to iterate over the entire array

Comments

0

A hacky solution to that could be to run arr.total.limited.things.platforms through a for loop and check the id tag to see if it's windows, if it is return value.

If you're using underscore.js -> _.each(arr.total.limited.things.platforms, function(x) { if (x.id === "Windows") { document.getElementById( "limited" ).value = x.value; } }); or a good old fashioned for loop will work too! Using this method, you can change the if statement to check for the wanted iphone/etc. ids and then increment the value.

Another solution to this could be to change the way your JSON is stored. So instead of making platforms an array, you could make it an object: platforms{ 'windows: { id = '', value = 1 }, etc. and then just call by key!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.