1

I have some JSON data in the "data" variable. FORMAT :

{
    "0" : {"names":"Pooja, Trivedi"},
    "1" : {"names":"Pooja, Rooster"}
}

I need to implement a map so as to count the different names:

Pooja = 2
Trivedi = 1
Rooster = 1

Here is the implementation:

map = new Array();
data = jQuery.parseJSON(data);  //convert JSON to object
for (var i = 0; i < data.length; i++) {
  var names = data[i].names.split(','); //split the CSVs
  for (var j = 0; j < names.length; j++) {
    if (map[names[j].trim()] === undefined) {
      map[names[j].trim()] = 1;
    } else {
      map[names[j].trim()]++;
    }
  }
console.log(map); //shows progressively filled arrays
}
console.log(map); //shows [], an empty array

Inside the loop, the map is updated. However after the end of the i loop, all we have is an empty array. How is this to be resolved?

14
  • should be object not array Commented Sep 20, 2015 at 20:11
  • 2
    @anurageldorado — No, it doesn't. data.length is undefined so it never enters the loop. You can see that on the live demo I linked to. Commented Sep 20, 2015 at 20:14
  • 2
    Do you have access to ES2015? You can use a new Map() which stores key/value pairs in the order they are added Commented Sep 20, 2015 at 20:14
  • 2
    I suspect this isn't your actual code, and somewhere in the conversion to an example, you lost something. If you set data to be a JSON representation of an array (rather than an object as you currently have), your code does run, but then the last console.log shows the the same as the previous, i.e. not [] as you say in the question Commented Sep 20, 2015 at 20:41
  • 1
    well then, as has been pointed out, the loop will never run because your object won't have a length property Commented Sep 20, 2015 at 20:44

2 Answers 2

2

First of all, you don't want to use for (var i = 0; i < data.length; i++) to traverse your data object, because a Javascript object doesn't have a length property like an array would. So, use a for (var i in data) instead, to traverse all the keys in the object.

This works:

var data = {
  "0" : {"names":"Pooja, Trivedi"},
  "1" : {"names":"Pooja, Rooster"}
}

var map = {};
for (var i in data) {
  var names = data[i].names.split(',');
  for (var j in names) {
    var name = names[j].trim();
    if (map[name]) {
      map[name]++;
    } else {
      map[name] = 1;
    }
  }
  console.log(map); //shows progressively filled arrays
}
console.log(map); //shows the full array

And you don't want to use an array as your map. You should instead use an object.

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

Comments

-1

I like this version better:

var data = {
    "0" : {"names":"Pooja, Trivedi"},
    "1" : {"names":"Pooja, Rooster"}
};

var nameCounts = {};

for (var item in data) {
    data[item].names.split(", ").forEach(function (name) {
        if (name in nameCounts) {
            nameCounts[name]++;
        } else {
            nameCounts[name] = 1;
        }
    });
}

console.log(nameCounts);

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.