javascript - Can't call specific value in Array that was created by JSON being .push(ed) to it? -


for reason struggling figure out how call specific values .push(ed) array json.

i have .push(key + ":" value); pushing array called tooltipvalues.

when tooltipvalues.join, can see populating in array but, array looks in console: ["value1:skill-one value1", "value2:skill-one value2", "value3:skill-one value3"] , isn't creating same model json. want able call tooltipvalues.value1 instance, haven't been able figure out how this. i've tried can think of.

is there way can keep json intact can call each individual value see fit? reason have setup way able @ data-tooltip tag in html, compare json , pull json matches data-tooltip.

you see below in js i'm doing , commented out, like so:

var tooltip = $("<div class='tooltip'>" + tooltipvalues.join("<br>") + "</div>")     //      // var tooltip = $("<div class='tooltip'>" + tooltipvalues.value1 + "</div><div>" + tooltipvalues.value2 + "</div>) 

here code:

tooltipdata = {    "skill-one": {      "value1": "skill-one value1",      "value2": "skill-one value2",      "value3": "skill-one value3"    },    "trinket-two": {      "value1": "trinket-two value1",      "value2": "trinket-two value2",      "value3": "trinket-two value3"    },  }    $('.skill, .trinket, .hero').hover(    function() {      //console.log( 'hovering on' , $(this).attr('tooltip') );       var tooltipjson = tooltipdata[$(this).data("tooltip")];      var tooltipvalues = [];            if (tooltipjson) {        $.each(tooltipjson, function(key, value) {          tooltipvalues.push(key + ":" + value);          console.log(tooltipvalues);        });      }      var tooltip = $("<div class='tooltip'>" + tooltipvalues.join("<br>") + "</div>")      //       // var tooltip = $("<div class='tooltip'>" + tooltipvalues.value1 + "</div><div>" + tooltipvalues.value2 + "</div>)        .css({          'color': '#fff',          'position': 'absolute',          'zindex': '99999',          'width': '100px',          'height': '150px',          'background-color': '#333',        });      $(this).append(tooltip);      $(document).on('mousemove', function(e) {        $('.tooltip').css({          left: e.pagex + 10,          top: e.pagey - 10        });      });    },    function() {      $('.tooltip').remove();    }  );
div {    width: 100px;    border: 1px solid #000;    padding: 10px;    margin: 5px;  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <div class='skill' data-tooltip="skill-one">skill one</div>  <div class='trinket' data-tooltip="trinket-two">trinket two</div>  <div class='hero' data-tooltip="hero-three">hero vestal</div>

unless i'm missing intermediate step, have object want it. defined tooltipdata contains objects , when var tooltipjson = tooltipdata[$(this).data("tooltip")];, object in format want use (it not json, js object)

in other words, can use tooltipvalues = tooltipdata[$(this).data("tooltip")] , use in commented out example

tooltipdata = {    "skill-one": {      "value1": "skill-one value1",      "value2": "skill-one value2",      "value3": "skill-one value3"    },    "trinket-two": {      "value1": "trinket-two value1",      "value2": "trinket-two value2",      "value3": "trinket-two value3"    },  }    $('.skill, .trinket, .hero').hover(    function() {      //console.log( 'hovering on' , $(this).attr('tooltip') );       var tooltipvalues= tooltipdata[$(this).data("tooltip")];            if(!tooltipvalues)return;      var tooltip = $("<div class='tooltip'><div>" + tooltipvalues.value1 + "</div><div>" + tooltipvalues.value2 + "</div></div>")        .css({          'color': '#fff',          'position': 'absolute',          'zindex': '99999',          'width': '100px',          'height': '150px',          'background-color': '#333',        });      $(this).append(tooltip);      $(document).on('mousemove', function(e) {        $('.tooltip').css({          left: e.pagex + 10,          top: e.pagey - 10        });      });    },    function() {      $('.tooltip').remove();    }  );
div {    width: 100px;    border: 1px solid #000;    padding: 10px;    margin: 5px;  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <div class='skill' data-tooltip="skill-one">skill one</div>  <div class='trinket' data-tooltip="trinket-two">trinket two</div>  <div class='hero' data-tooltip="hero-three">hero vestal</div>


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -