1

I am trying to get a data object when I click the div which has different attribute and here is some example:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
var a = {
        "from Japan" : {"air" : "1 hour 30 min", "train" : null, "bus" : null},
        "from HongKong" : {"air" : "1 hour 45 min", "train" : null, "bus" : null},
        "from Taiwan" : {"air" : "2 hour 10 min", "train" : null, "bus" : null}
}

var b = {
        "from Japan" : {"air" : "1 hour 20 min", "train" : "3 hour 20 min", "bus" : null},
        "from HongKong" : {"air" : null, "train" : null, "bus" : null},
        "from Taiwan" : {"air" : null, "train" : null, "bus" : null}
}

var c = {
        "from Japan" : {"air" : null, "train" : "2 hour 20 min", "bus" : "7 hour 25min"},
        "from HongKong" : {"air" : null, "train" : null, "bus" : null},
        "from Taiwan" : {"air" : null, "train" : null, "bus" : null}
}

var d = {
        "from Japan" : {"air" : null, "train" : "1 hour 36 min", "bus" : "5 hour 27min"},
        "from HongKong" : {"air" : null, "train" : null, "bus" : null},
        "from Taiwan" : {"air" : null, "train" : null, "bus" : null}
}

$('#hoge ul li').click(function() {
    var place = $(this).attr('title');
    var message = "";
        message += "<b>From Japan</b>" + '<br />';
        message += fromArea(place.Japan);
        message += "<b>From HongKong</b>" + '<br />';
        message += fromArea(place.HongKong);
        message += "<b>From Taiwan</b>" + '<br />';
        message += fromArea(place.Taiwan);
        $('#fuga').html( message );

    function fromArea(location) {
      var output = "";
      if(location.air != null){
        output += 'Airplane : ' + location.air + '<br />';
      }
      if(location.train != null){
        output += 'Train : ' + location.train + '<br />';
      }
      if(location.bus != null){
        output += 'Bus : ' + location.bus + '<br />';
      }
      return output;
    }
});
</script>


<div id="hoge">
    <ul>
        <li title="a">
            Place A
        </li>
        <li title="b">
            Place B
        </li>
        <li title="c">
            Place C
        </li>
        <li title="d">
            Place D
        </li>
    </ul>
</div>

<div id="fuga"></div>

Expected result when I click title="a"

From Japan

Airplane : 1 hour 30 min

From HongKong

Airplane : 1 hour 45min

From Taiwan

Airplane : 2 hour 10min

I want to get a different results when I click title="b" title="c" title="d"

however I could not get a data from different titles..... please give me an idea?

added

Since I got the correct result, I'd like to develop this plugin.

I's like to hide fromArea message if any of the area has null result.

example:

current result

From Japan
Train : 1 hour 36 min
Bus : 5 hour 27min
From HongKong
From Taiwan

to

From Japan
Train : 1 hour 36 min
Bus : 5 hour 27min

Another Update

I've tried @vrluckyin 's method but seems like something wrong with my data objects.....?!?!

var Hokkaido = {
        'Tokyo' : {'air' : '1 hour 30 min', 'train' : null, 'bus' : null},
        'Osaka' : {'air' : '1 hour 45 min', 'train' : null, 'bus' : null},
        'Fukuoka' : {'air' : '2 hours 10 min', 'train' : null, 'bus' : null},
}
var Aomori = {
        'Tokyo' : {'air' : '1 hours 20 min', 'train' : '3 hours 20 min', 'bus' : null},
        'Osaka' : {'air' : null, 'train' : null, 'bus' : null},
        'Fukuoka' : {'air' : null, 'train' : null, 'bus' : null}
}

var Iwate = {
        "Tokyo" : {"air" : null, "train" : "2 hours 20 min", "bus" : "7 hours 25 min"},
        "Osaka" : {"air" : null, "train" : null, "bus" : null},
        "Fukuoka" : {"air" : null, "train" : null, "bus" : null}
}

var Miyagi = {
        "Tokyo" : {"air" : null, "train" : "1 hour 36 min", "bus" : "5 hours 27 min"},
        "Osaka" : {"air" : null, "train" : null, "bus" : null},
        "Fukuoka" : {"air" : null, "train" : null, "bus" : null}
}

$('#hoge ul li').click(function() {

  var place = $(this).attr('title');
  if (place != undefined) {
    var message = "";
    message += fromArea('Tokyo', place);
    message += fromArea('Osaka', place);
    message += fromArea('Fukuoka', place);
    $('#fuga').html(message);
  }

  function fromArea(name, location) {
    location = eval(location + '.' + name);
    var output = "";
    if(location.air != null) {
        output += 'Airplane : ' + location.air + '<br />';
    }
    if(location.train != null) {
        output += 'Train : ' + location.air + '<br />';
    }
    if(location.bus != null) {
        output += 'Bus : ' + location.air + '<br />';
    }
    if(output != "") {
        output = "<b> From " + name +"</b>" + "<br />" + output;
    }
    return output;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="hoge">
  <ul>
    <li title='Hokkaido'>
      Hokkaido
    </li>
    <li title='Aomori'>
      Aomori
    </li>
    <li title='Iwate'>
      Iwate
    </li>
    <li title='Miyagi'>
      Miyagi
    </li>
  </ul>
</div>
<div id="fuga"></div>

1
  • The problem is when you're calling fromArea(place.Japan), place is not variable a but the string value "a". So place.Japan will be undefined. Commented Feb 25, 2015 at 5:19

4 Answers 4

1

You may like this solution:

$('#hoge ul li').click(function() {
    var place = $(this).attr('title');
    var message = "";
        message = fromArea(place);
        $('#fuga').html( message );

    function fromArea(loc) {
        var name = "";
        var result = "";
        for (var key in a) {
            name = key;
            var location = eval(loc+'.'+name);
            var output = "";
            if(location.air != null){
                output += 'Airplane : ' + location.air + '<br />';
            }
            if(location.train != null){
                output += 'Train : ' + location.train + '<br />';
            }
            if(location.bus != null){
                output += 'Bus : ' + location.bus + '<br />';
            }
            if(output!=""){
                result += "<b>From "+name+ "</b>" + '<br />' + output;
            }

        }
        return result;
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

this is very clean code. how can i define the title attributes?
0

This script might be helpful after a little modification in your json data:

http://jsfiddle.net/mdqmo25m/

var a = {
        "Japan" : {"air" : "1 hour 30 min", "train" : null, "bus" : null},
        "HongKong" : {"air" : "1 hour 45 min", "train" : null, "bus" : null},
        "Taiwan" : {"air" : "2 hour 10 min", "train" : null, "bus" : null}
}

var b = {
        "Japan" : {"air" : "1 hour 20 min", "train" : "3 hour 20 min", "bus" : null},
        "HongKong" : {"air" : null, "train" : null, "bus" : null},
        "Taiwan" : {"air" : null, "train" : null, "bus" : null}
}

var c = {
        "Japan" : {"air" : null, "train" : "2 hour 20 min", "bus" : "7 hour 25min"},
        "HongKong" : {"air" : null, "train" : null, "bus" : null},
        "Taiwan" : {"air" : null, "train" : null, "bus" : null}
}

var d = {
        "Japan" : {"air" : null, "train" : "1 hour 36 min", "bus" : "5 hour 27min"},
        "HongKong" : {"air" : null, "train" : null, "bus" : null},
        "Taiwan" : {"air" : null, "train" : null, "bus" : null}
}

    $('#hoge ul li').click(function() {
        var place = $(this).attr('title');
        var message = "";
            message += "<b>From Japan</b>" + '<br />';
            message += fromArea(eval(place+'.Japan'));
            message += "<b>From HongKong</b>" + '<br />';
            message += fromArea(eval(place+'.HongKong'));
            message += "<b>From Taiwan</b>" + '<br />';
            message += fromArea(eval(place+'.Taiwan'));
            $('#fuga').html( message );

        function fromArea(location) {
          var output = "";
          if(location.air != null){
            output += 'Airplane : ' + location.air + '<br />';
          }
          if(location.train != null){
            output += 'Train : ' + location.train + '<br />';
          }
          if(location.bus != null){
            output += 'Bus : ' + location.bus + '<br />';
          }
          return output;
        }
    });

EDIT:

Based on your comment, below snap outputs result as per your expectation:

$('#hoge ul li').click(function() {
    var place = $(this).attr('title');

     var message = "";
        message += fromArea('Japan',place);
        message += fromArea('HongKong',place);
        message += fromArea('Taiwan',place);
        $('#fuga').html( message );

    function fromArea(name,location) {
      location = eval(location+'.'+name);
      var output = "";
      if(location.air != null){
        output += 'Airplane : ' + location.air + '<br />';
      }
      if(location.train != null){
        output += 'Train : ' + location.train + '<br />';
      }
      if(location.bus != null){
        output += 'Bus : ' + location.bus + '<br />';
      }
      if(output!="")
      {
        output = "<b>From "+name+ "</b>" + '<br />' + output;
      }
      return output;
    }
});

5 Comments

Ahhhh... I did not add eval() to the variable. Now I figured out and worked and thank you so much.
I was just wondering is there anyways that I can simplify the result? If any of the fromArea has 'null', i do not want to display the area name as well. Any tip would be appreciated.
Thank you for the update. I updated the result but looks like there are something wrong with my data objects..... I think those are valid. no?
I've verified your data and exactly matches with output - reference: screencast.com/t/BooR3blTp . Would like to know other data points if you are running code against those data.
yeah, it looks fine on the video that you sent me. would you please check the "Another Update" code and run the snippet? Hokkaido data looks okay but it gets wrong data after the first one.
0

This is because the attribute is just going to be a string, "a", "b", "c", etc not the object stored in the variable with the same name. So instead of separate variables make the data separate properties of an object and use the attribute value as a key

var places = {
    a:{
        "Japan" : {"air" : "1 hour 30 min", "train" : null, "bus" : null},
        "HongKong" : {"air" : "1 hour 45 min", "train" : null, "bus" : null},
        "Taiwan" : {"air" : "2 hour 10 min", "train" : null, "bus" : null}
    },
    b:{
        "Japan" : {"air" : "1 hour 20 min", "train" : "3 hour 20 min", "bus" : null},
        "HongKong" : {"air" : null, "train" : null, "bus" : null},
        "Taiwan" : {"air" : null, "train" : null, "bus" : null}
    },
    c:{
        "Japan" : {"air" : null, "train" : "2 hour 20 min", "bus" : "7 hour 25min"},
        "HongKong" : {"air" : null, "train" : null, "bus" : null},
        "Taiwan" : {"air" : null, "train" : null, "bus" : null}
    },
    d:{
        "Japan" : {"air" : null, "train" : "1 hour 36 min", "bus" : "5 hour 27min"},
        "HongKong" : {"air" : null, "train" : null, "bus" : null},
        "Taiwan" : {"air" : null, "train" : null, "bus" : null}
    }
};

var key = $(this).attr('title');
var place = places[key];

Also note i took out the "from " part in the object properties so that they can be accessed like place.Japan otherwise you would have to use bracket notation like place["from Japan"] to correctly get the properties.

Demo

var places = {
  a:{
    "Japan" : {"air" : "1 hour 30 min", "train" : null, "bus" : null},
    "HongKong" : {"air" : "1 hour 45 min", "train" : null, "bus" : null},
    "Taiwan" : {"air" : "2 hour 10 min", "train" : null, "bus" : null}
  },
  b:{
    "Japan" : {"air" : "1 hour 20 min", "train" : "3 hour 20 min", "bus" : null},
    "HongKong" : {"air" : null, "train" : null, "bus" : null},
    "Taiwan" : {"air" : null, "train" : null, "bus" : null}
  },
  c:{
    "Japan" : {"air" : null, "train" : "2 hour 20 min", "bus" : "7 hour 25min"},
    "HongKong" : {"air" : null, "train" : null, "bus" : null},
    "Taiwan" : {"air" : null, "train" : null, "bus" : null}
  },
  d:{
    "Japan" : {"air" : null, "train" : "1 hour 36 min", "bus" : "5 hour 27min"},
    "HongKong" : {"air" : null, "train" : null, "bus" : null},
    "Taiwan" : {"air" : null, "train" : null, "bus" : null}
  }
};

$('#hoge ul li').click(function() {
    var key = $(this).attr('title');
    var place = places[key];
    var message = "";
        message += "<b>From Japan</b>" + '<br />';
        message += fromArea(place.Japan);
        message += "<b>From HongKong</b>" + '<br />';
        message += fromArea(place.HongKong);
        message += "<b>From Taiwan</b>" + '<br />';
        message += fromArea(place.Taiwan);
        $('#fuga').html( message );

    function fromArea(location) {
      var output = "";
      if(location.air != null){
        output += 'Airplane : ' + location.air + '<br />';
      }
      if(location.train != null){
        output += 'Train : ' + location.train + '<br />';
      }
      if(location.bus != null){
        output += 'Bus : ' + location.bus + '<br />';
      }
      return output;
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="hoge">
    <ul>
        <li title="a">
            Place A
        </li>
        <li title="b">
            Place B
        </li>
        <li title="c">
            Place C
        </li>
        <li title="d">
            Place D
        </li>
    </ul>
</div>

<div id="fuga"></div>

1 Comment

Beat me to the submit button, but you have covered all that I wanted to write and more! Good answer!
0

Below code snippet generates output that matches with your case.

NOTE: Most of people say that eval is evil, isn't it?

http://jsfiddle.net/gr95k75s/

var message = "";
        message += "<b>From Japan</b>" + '<br />';
        message += fromArea(eval(place+'["from Japan"]'));
        message += "<b>From HongKong</b>" + '<br />';
        message += fromArea(eval(place+'["from HongKong"]'));
        message += "<b>From Taiwan</b>" + '<br />';
        message += fromArea(eval(place+'["from Taiwan"]'));

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.