Javascript-在数组中定位特定索引

时间:2018-08-03 10:05:15

标签: javascript

我对javascript非常陌生,非常感谢您的帮助。

germany
france
uk
italy
spain
poland
romania
netherlands
belgium
greece


clickData.points[0].label
//germany [console: tells what country was clicked on - as shown in the image]

var iExpandPos = clickData.points[0].fullData.labels.indexOf(clickData.points[0].label)
//2 [console: tells the index position of the country]

var len = clickData.points[0].fullData.labels.length;
//10 [console: gives the total number of countries listed]

我想做的是:

如果点击了某个国家(例如德国),请制作pull = 0.2和 对于其他国家/地区,pull应该是pull = 0.1,但是由于下面的代码一直在中断,我不确定如何编写此代码。您的建议会非常有帮助。

var myPlot = document.getElementById(chartDivID);
console.log('myPlot:------------>', myPlot.data[0])
clickData = data;
var iExpandPos = clickData.points[0].fullData.labels.indexOf(clickData.points[0].label)
var len = clickData.points[0].fullData.labels.length;
var pts = "";
for (var i = 0; i < data.points.length; i++) {
    pts = 'country= ' + clickData.points[0].label + '\nvalue = ' + clickData.points[0].value + '\nposition = ' + iExpandPos;


    int[] pull = new int[](len);
    for (i = 0; i = len) {
        if (i === iExpandPos) {
            pull[i] = 0.2
        }
        else {
            pull[i] = 0.1
        }
    }

    console.log('check:------->', pts)
}

enter image description here

1 个答案:

答案 0 :(得分:1)

不需要第二个for循环,创建数组pull的方式是错误的。这是您的代码的更正:

var myPlot = document.getElementById(chartDivID);
console.log('myPlot:------------>', myPlot.data[0]);
clickData = data;
var iExpandPos = clickData.points[0].fullData.labels.indexOf(clickData.points[0].label);
var len = clickData.points[0].fullData.labels.length;
var pts = "";
var pull = [];
for (var i = 0; i < data.points.length; i++) {
  pts = 'country= ' + clickData.points[i].label + '\nvalue = ' + clickData.points[i].value + '\nposition = ' + iExpandPos;

  var val = i === iExpandPos ? 0.2 : 0.1;
  pull.push(val);

  console.log('check:------->', pts);
}