考虑以下几点:创建`option`用下划线
function autoCompleteOptions(json) {
_.map(json, function(x) {
const a = x["a"];
const b = x["b"];
var option = document.createElement('option');
option.setAttribute('data-value', a);
option.value = b;
console.log('option:', option);
return option;
})
}
var result = autoCompleteOptions([
{
"a" : "hi",
"b" : "world"
}
]);
console.log('result:', result);
<script src="http://underscorejs.org/underscore-min.js"></script>
为什么console.log
显示undefined
?我期待着一组option
HTML元素。
因为你的函数('autoCompleteOptions')没有明确返回值? – undefined
在你的代码片段中,它看起来像你不包含'underscore.js' –
这段代码中没有JSON。 JSON是一种将数据编码为字符串的格式。你有一个数组和一个对象。 – 4castle