我想得到一个像这样的对象:
{"Red 1":53,"Blue 2":26,"Green 3":25}
从以下示例:
试图将 .each 内部的数据推送到对象,但它是多维的,我不知道如何实现这一点:
//html
<div class="test">
<div class="color">Red 1</div>
<div class="value">53</div>
</div>
<div class="test">
<div class="color">Blue 2</div>
<div class="value">26</div>
</div>
<div class="test">
<div class="color">Green 3</div>
<div class="value">25</div>
</div>
//js
var dataPoints = {};
var colorTitle = '';
var colorValue = '';
$('.test').each(function(index) {
colorTitle = $(this).find('.color').html();
colorValue = $(this).find('.value').html();
dataPoints.push({colorTitle:colorValue});
});
上面的代码显然不起作用,但我想基本上展示我正在尝试做的事情。
也尝试过这种方法:
dataPoints[index][colorTitle] = colorValue;
这也不起作用。可能一起遗漏了一些东西,但任何帮助表示赞赏!谢谢