3

I am currently trying to get the values from a dictionary array, without knowing the key.

[{
    "msmittens007": 39717783
}, {
    "starboynaseem321": 60806140
}, {
    "creeper333888": 32048827
}, {
    "simontax": 52222895
}, {
    "marissahill55": 44062186
}, {
    "tgibs1": 52704223
}, {
    "angelsunrise7": 57944274
}, {
    "nicks24": 51306000
}, {
    "opticbomb": 21804587
}, {
    "KhloeTheMoaiOwie": 26598082
}]

My current JSON. I need to be able to get the value attached to the key. I am new to JSON so it is hard for me.

Sorry if a bit ambiguous, but how would I access the value without the key? e.g. instead of jsonTable["msmittens007"] to get 39717783, I need to be able to do something like jsonTable[1] = 39717783

I hope you understand.

Thanks!

Wand Maker
  • 18,476
  • 8
  • 53
  • 87
Max
  • 33
  • 3
  • 3
    That's a pretty bad data structure to begin with, precisely because of the problem you point out. You should have defined keys inside the dictionaries, or you should just have one dictionary instead of an array of them. Can *you* change the data structure? – deceze Dec 01 '15 at 13:56
  • Yeah. It's not my API, so no. – Max Dec 01 '15 at 14:00
  • 1
    `jsonTable["msmittens007"]` returns undefined, `jsonTable[0]["msmittens007"]`returns 39717783 – Ruan Mendes Dec 01 '15 at 14:02
  • Javascript doesn't have a dictionary – Liam Dec 01 '15 at 14:04
  • @JuanMendes Sorry, not the actual code I tried. It should have been jsonTable[0], yeah. – Max Dec 01 '15 at 14:06
  • It sounds like you want this [Access non-numeric Object properties by index?](http://stackoverflow.com/questions/7866275/access-non-numeric-object-properties-by-index) – Liam Dec 01 '15 at 14:09
  • @Max so you wanted to turn an array of single key objects into an array of integers (the value of each object). I'm really curious as to how that is going to be useful to you. – Ruan Mendes Dec 01 '15 at 14:15

6 Answers6

2

You would need to map the array on objects to an array

var arr = [{"msmittens007":39717783},{"starboynaseem321":60806140},
           {"creeper333888":32048827},{"simontax":52222895},
           {"marissahill55":44062186},{"tgibs1":52704223},
           {"angelsunrise7":57944274},{"nicks24":51306000},
           {"opticbomb":21804587},{"KhloeTheMoaiOwie":26598082}];

var updated = arr.map(function(obj){  //map returns a new array based on what you return
    var keys = Object.keys(obj);  //get the keys for this object
    return obj[keys[0]];          //return the property value for this object
});

console.log(updated[0]);
epascarello
  • 204,599
  • 20
  • 195
  • 236
1

Well, I saw you JSON structure and below is the way to access all the values from the JSON:

    var data = [{"msmittens007":39717783},{"starboynaseem321":60806140},{"creeper333888":32048827},{"simontax":52222895},{"marissahill55":44062186},{"tgibs1":52704223},{"angelsunrise7":57944274},{"nicks24":51306000},{"opticbomb":21804587},{"KhloeTheMoaiOwie":26598082}];

    var valueCollection = []; 
      for (var i= 0; i<data.length; i++){
          for(var key in data[i]){ 
          var value = data[i][key]; // you can use this value      
           valueCollection.push(value);  // for sample pushing into array you can use as per you need here
           }
        }

valueCollection is the sample array which will contains all the values.

Hope this will help you :)

Vikash Pandey
  • 5,407
  • 6
  • 41
  • 42
1

Turn your array into a map if you need to access the keys directly. I'm suggesting this instead of what you asked because there's a chance that this object is a C# dictionary because that's how .NET serializes them, so I've used this before to turn the .NET's JSON dictionary into a regular object

var arr = [{"msmittens007":39717783},{"starboynaseem321":60806140},{"creeper333888":32048827},{"simontax":52222895},{"marissahill55":44062186},{"tgibs1":52704223},{"angelsunrise7":57944274},{"nicks24":51306000},{"opticbomb":21804587},{"KhloeTheMoaiOwie":26598082}];


 var obj = arr.reduce(function(prev, next) {
    var key = Object.keys(next)[0];
    prev[key] = next[key]; 
    return prev;
 },{});

console.log(obj.msmittens007); // 39717783
console.log(obj.starboynaseem321); // 60806140

Turn it into an array of the values if you don't care about the keys. I do wonder how this could be useful...

    var arr = [{"msmittens007":39717783},{"starboynaseem321":60806140},{"creeper333888":32048827},{"simontax":52222895},{"marissahill55":44062186},{"tgibs1":52704223},{"angelsunrise7":57944274},{"nicks24":51306000},{"opticbomb":21804587},{"KhloeTheMoaiOwie":26598082}];

var valuesOnly = arr.map(function(item){
    return item[Object.keys(item)[0]];
});

console.log(valuesOnly);
// [39717783, 60806140, 32048827, 52222895, 44062186, 52704223, 57944274, 51306000, 21804587, 26598082]
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
0

You can map it to an object:

var hash = {};

for(var i=0; i<json.length; i++) {
    for(var k in json[i]) {
        hash[k] = json[i][k];
    }
}

return hash['msmittens007'];
Master Morality
  • 5,837
  • 6
  • 31
  • 43
  • Basically, I need to be able to access it with numbering instead of with a key. e.g. return hash[1]; That is the only way my program could work, since the useful bit is the number bit. – Max Dec 01 '15 at 14:04
0

to be able to do something like jsonTable[1] = 39717783..

you can remap it:

var arr = [{
    "msmittens007": 39717783
}, {
    "starboynaseem321": 60806140
}, {
    "creeper333888": 32048827
}, {
    "simontax": 52222895
}, {
    "marissahill55": 44062186
}, {
    "tgibs1": 52704223
}, {
    "angelsunrise7": 57944274
}, {
    "nicks24": 51306000
}, {
    "opticbomb": 21804587
}, {
    "KhloeTheMoaiOwie": 26598082
}];

var mapped =arr.map(x=> x[Object.keys(x)[0]])
console.log(    mapped[0]    )
maioman
  • 18,154
  • 4
  • 36
  • 42
0

As @deceze pointed out in the comment section, your data structure looks pretty weird, it's a list of key-value pairs, not quite the same as a dictionary.

Anyway, if you want to access the values via numeric keys, you can use Array#map to do so.

var json = [{"msmittens007":39717783},{"starboynaseem321":60806140},{"creeper333888":32048827},{"simontax":52222895},{"marissahill55":44062186},{"tgibs1":52704223},{"angelsunrise7":57944274},{"nicks24":51306000},{"opticbomb":21804587},{"KhloeTheMoaiOwie":26598082}];

var jsonTable = json.map(function (pair) { 
  var keys = Object.keys(pair);
  return pair[keys[0]];
});

console.log(jsonTable[0]); // 39717783