使用 jquery 根据数组中的对象值设置列表项的宽度

Set width of list items based on object values in an array using jquery

我有一个数组,里面有一组对象。

var country_data = [
{"country_id":1,"country":"Luxembourg","local_wage":"407","wage":"489","exchange":"0.98"},
{"country_id":2,"country":"Norway","local_wage":"3200","wage":"378","exchange":"9.57"},
{"country_id":3,"country":"Austria","local_wage":"290","wage":"337","exchange":"0.87"},
{"country_id":4,"country":"United States","local_wage":"363","wage":"363","exchange":"1"},
{"country_id":5,"country":"United Kingdom","local_wage":"284","wage":"365","exchange":"0.68"}
];

我还有一个 for 循环,它从每个对象中检索第三个值(工资):

for (var i=0; i<country_data.length; i++){
    var wage = country_data[i].wage;
    $('.countries.country_'+i).css('width', wage+'px');

我想使用 .css、宽度 属性.

将该图形的值附加到相应列表元素的宽度
<ul class="countries">
<li class="country_1">test</li>
<li class="country_2">test</li>
<li class="country_3">test</li>
<li class="country_4">test</li>
<li class="country_5">test</li>
</ul>

我可以让它遍历数组但不追加到相应的列表元素。感谢任何帮助,谢谢。

你的选择器有误,应该是

$('.countries .country_' + i)

$('.country_' + i)

你的 i 参数也是错误的。它以 0 表示。您最有可能应该使用 country_data[i].country_id