gpt4 book ai didi

javascript - 将外部 JSON URL 标签和值拉入新数组

转载 作者:行者123 更新时间:2023-11-28 05:50:42 25 4
gpt4 key购买 nike

在我的 fiddle 中http://jsfiddle.net/UGYzW/319/数据目前来自本地源,简称为“数据”。我如何从外部值中提取数据?请使用 http://echo.jsontest.com/key/value/one/two并从第一个登录项中拉回值“mralexgray”。我们是否需要将项目推送到新数组?

最后,使用 JSONP、JSON 还是 .AJAX 哪个更好?

  var data =[
{'label':'Core','value':1},
{'label':' Selectors','value':2},
{'label':'Events' ,'value':3}];

var nameArray = data.map(function(item){
return {value: item.value, label: item.label};
});


$("#meta-area").autocomplete({
source:nameArray,
select: function(e, ui) {
e.preventDefault() // <--- Prevent the value from being inserted.
$("#meta_search_ids").val(ui.item.label);

$(this).val(ui.item.value);
}
});
//alert("this loaded");

最佳答案

由于您将 data 变量创建为 Array,因此在 ajax 响应后将所有变量推送到数组中。稍后,同一脚本将与 map 函数一起使用

jsfiddle example这里

 $.ajax({
url:'https://api.github.com/users/mralexgray/repos',
success:function(data) {
var dataArray = [];
for(var i=0;i<data.length;i++){
dataArray.push(data[i]);
console.log(data[i]);
}
var nameArray = dataArray.map(function(item){
return {value: item.owner, label: item.name};
});

$("#meta-area").autocomplete({
source:nameArray,
select: function(e, ui) {
e.preventDefault();
$("#meta_search_ids").val(ui.item.label);
$(this).val(ui.item.value);
}
});
}
});

关于javascript - 将外部 JSON URL 标签和值拉入新数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38102047/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com