2

I am trying to parse a json object from a variable in an each() object.

For example I have this long array:

{castle: "0", commercial: "3", common: "0", cultural: "2", dioni: "0", holidays: "0", island: "2", nereides: "2", others: "0", peninsula: "0", thetis: "0"}

and I have div's with the same classes as those keys of the array. For example:

<div class="castle"></div>
<div class="commercial"></div>
<div class="common"></div>
<div class="cultural"></div>

etc.

So I want to append the value of each key to the class where class = key.

$("body div").each(function(){
   var array = [] // the reall long array;

   var a = $(this).attr("class").split(' ')[0]; // in case there are two divs, split
   $("." + a).text(array.a + " hours"); // array.a doesn't work

});

In this case, array.a doesn't not exist in the array so I cannot parse the value of each key to each class.

How can I solve this?

jQuerybeast
  • 14,130
  • 38
  • 118
  • 196
  • @KhanhTO array["a"] won't work. I am trying to parse the variable a. Not the object a – jQuerybeast Aug 03 '13 at 08:50
  • @Quentin not a duplicate. – jQuerybeast Aug 03 '13 at 08:55
  • Variables `a` is a string containing a class name. It isn't any form of serialised data. It isn't something you can parse. The code you have in the question makes it look very much like you are trying to do exactly the same as the duplicate question. – Quentin Aug 03 '13 at 08:59
  • Can you not parse a string? – jQuerybeast Aug 03 '13 at 09:01
  • @Quentin this is my entire code: http://pastebin.com/TpWLFZDf I am not trying to do what that "duplicated" question is trying to do. Maybe something a bit similar, but it is a different question which could have different solutions to that duplicate; which I believe it deserves its on page for answers – jQuerybeast Aug 03 '13 at 09:02
  • @Quentin Or maybe it could have the same answer, but from a completely different approach. – jQuerybeast Aug 03 '13 at 09:04
  • 1
    It's exactly the same. You have the property name in a variable `a`, hence you have to use `obj[a]` to access the property value. `obj['a']` would try to access the property with name `a` which is wrong of course. – Felix Kling Aug 03 '13 at 10:23

3 Answers3

3

Why dont your do it the other way? Use a for..in to loop (per se) through the object and set it up :

for (var key in obj) {
    $("div." + key).text(obj[key]);
}

The selector inside this loop will find the div with the class which is equal to the key and append the corresponding value to it.

Demo : http://jsfiddle.net/hungerpain/9z4zj/

But, looking at your complication, I concur that your way is the easiest. Just change array.a to array[a]. That's all you need.

krishwader
  • 11,341
  • 1
  • 34
  • 51
0

Try this:

$(document).ready(function () {
    $("body div").each(function () {
        var myarray = {
            castle: "0",
            commercial: "3",
            common: "0",
            cultural: "2",
            dioni: "0",
            holidays: "0",
            island: "2",
            nereides: "2",
            others: "0",
            peninsula: "0",
            thetis: "0"
        }; // the reall long array;

        var a = $(this).attr("class").split(' ')[0]; // in case there are two divs, split
        $("." + a).html(myarray[a] + " hours"); // array.a doesn't work
    });
});  

JSFIDDLE DEMO

Dhaval Bharadva
  • 3,053
  • 2
  • 24
  • 35
0

Use array[a] instead of array.a

var obj = {castle: "0", commercial: "3", common: "0", cultural: "2", dioni: "0", holidays: "0", island: "2", nereides: "2", others: "0", peninsula: "0", thetis: "0"}

$("body div").each(function(){
   var array = [] // the reall long array;

   var a = $(this).attr("class").split(' ')[0]; // in case there are two divs, split

   $("." + a).text(obj[a] + " hours"); // array.a doesn't work

});

Working demo : http://jsfiddle.net/uNrKy/

Nishu Tayal
  • 20,106
  • 8
  • 49
  • 101