早上好。 我试图在页面加载时将json对象中的值添加到数组中。我尝试使用for和in循环遍历它,但我无法找到视频名称。
-ajax呼叫-
$.ajax({
url: "/Search/GetVideoNamesForAutoComplete",
type: "GET",
dataType: "json",
//data: function () {
// var data = new FormData();
// data.append("videoName", $(this).val());
// return data;
//}(),
success: function (data) {
var listofVideos = data;
var objects;
var sourceArray = [];
var vidNames = [];
for (var i = 0; i < listofVideos.length; i++) {
vidNames.push(listOfVideos[i].videoName);
}
console.log(listofVideos);
jQuery('#autosearch').autocomplete({
source: sourceArray
});
},
error: function (jqXHR, textStatus, errorMessage) {
console.log(errorMessage);
}
});
-对象-
listofVideos: Array(7)
0: {videoID: 0, videoName: "Kikoushi_Enma-01", createdbyUserID: 0, deletedByUserID: null, createdDateTime: null, …}
1: {videoID: 0, videoName: "Higurashi_no_Nakukoroni_01", createdbyUserID: 0, deletedByUserID: null, createdDateTime: null, …}
2: {videoID: 0, videoName: "Login Page - Internet Explorer 11_10_2018 9_06_09 PM", createdbyUserID: 0, deletedByUserID: null, createdDateTime: null, …}
3: {videoID: 0, videoName: "3killsmp4", createdbyUserID: 0, deletedByUserID: null, createdDateTime: null, …}
4: {videoID: 0, videoName: "Login Page - Google Chrome 10_28_2018 8_07_12 PM_Trim.mp4", createdbyUserID: 0, deletedByUserID: null, createdDateTime: null, …}
5: {videoID: 0, videoName: "Candle - 4101", createdbyUserID: 0, deletedByUserID: null, createdDateTime: null, …}
6: {videoID: 0, videoName: "Northernlights2_HD.mp4", createdbyUserID: 0, deletedByUserID: null, createdDateTime: null, …}
length: 7
控制器功能
[HttpGet]
public JsonResult GetVideoNamesForAutoComplete()
{
DataLayer dl = new DataLayer();
NewVideosModel listOfVideoModels = new NewVideosModel();
listOfVideoModels.listofVideos = new List<VideoModel>();
DataTable dt = dl.GetVideoNamesForAutoComplete();
for (int i = 0; i < dt.Rows.Count; i++)
{
VideoModel videoModel = new VideoModel();
videoModel.VideoName = dt.Rows[i]["VideoName"].ToString();
listOfVideoModels.listofVideos.Add(videoModel);
}
return Json(listOfVideoModels);
}
-console输出listOfVideoModels ---
{listofVideos: Array(7)}listofVideos: Array(7)0: {videoID: 0, videoName: "Kikoushi_Enma-01", createdbyUserID: 0, deletedByUserID: null, createdDateTime: null, …}1: {videoID: 0, videoName: "Higurashi_no_Nakukoroni_01", createdbyUserID: 0, deletedByUserID: null, createdDateTime: null, …}2: {videoID: 0, videoName: "Login Page - Internet Explorer 11_10_2018 9_06_09 PM", createdbyUserID: 0, deletedByUserID: null, createdDateTime: null, …}3: {videoID: 0, videoName: "3killsmp4", createdbyUserID: 0, deletedByUserID: null, createdDateTime: null, …}4: {videoID: 0, videoName: "Login Page - Google Chrome 10_28_2018 8_07_12 PM_Trim.mp4", createdbyUserID: 0, deletedByUserID: null, createdDateTime: null, …}5: {videoID: 0, videoName: "Candle - 4101", createdbyUserID: 0, deletedByUserID: null, createdDateTime: null, …}6: {videoID: 0, videoName: "Northernlights2_HD.mp4", createdbyUserID: 0, deletedByUserID: null, createdDateTime: null, …}length: 7__proto__: Array(0)__proto__: Object`
答案 0 :(得分:0)
您可以使用.map将值映射到数组中。见下文:
var listOfVideos = [{videoID: 0, videoName: "Kikoushi_Enma-01", createdbyUserID: 0, deletedByUserID: null, createdDateTime: null}
,{videoID: 0, videoName: "Higurashi_no_Nakukoroni_01", createdbyUserID: 0, deletedByUserID: null, createdDateTime: null}
,{videoID: 0, videoName: "Login Page - Internet Explorer 11_10_2018 9_06_09 PM", createdbyUserID: 0, deletedByUserID: null, createdDateTime: null}
,{videoID: 0, videoName: "3killsmp4", createdbyUserID: 0, deletedByUserID: null, createdDateTime: null}
,{videoID: 0, videoName: "Login Page - Google Chrome 10_28_2018 8_07_12 PM_Trim.mp4", createdbyUserID: 0, deletedByUserID: null, createdDateTime: null}
,{videoID: 0, videoName: "Candle - 4101", createdbyUserID: 0, deletedByUserID: null, createdDateTime: null}
,{videoID: 0, videoName: "Northernlights2_HD.mp4", createdbyUserID: 0, deletedByUserID: null, createdDateTime: null}];
var videoNames = listOfVideos.map(v => v.videoName);
console.log(videoNames);
或者您可以对其进行迭代:
var listOfVideos = [{videoID: 0, videoName: "Kikoushi_Enma-01", createdbyUserID: 0, deletedByUserID: null, createdDateTime: null}
,{videoID: 0, videoName: "Higurashi_no_Nakukoroni_01", createdbyUserID: 0, deletedByUserID: null, createdDateTime: null}
,{videoID: 0, videoName: "Login Page - Internet Explorer 11_10_2018 9_06_09 PM", createdbyUserID: 0, deletedByUserID: null, createdDateTime: null}
,{videoID: 0, videoName: "3killsmp4", createdbyUserID: 0, deletedByUserID: null, createdDateTime: null}
,{videoID: 0, videoName: "Login Page - Google Chrome 10_28_2018 8_07_12 PM_Trim.mp4", createdbyUserID: 0, deletedByUserID: null, createdDateTime: null}
,{videoID: 0, videoName: "Candle - 4101", createdbyUserID: 0, deletedByUserID: null, createdDateTime: null}
,{videoID: 0, videoName: "Northernlights2_HD.mp4", createdbyUserID: 0, deletedByUserID: null, createdDateTime: null}];
var vidNames = [];
for(var i = 0; i < listOfVideos.length; i++){
vidNames.push(listOfVideos[i].videoName);
}
console.log(vidNames);
两个选项都可以正常工作。
答案 1 :(得分:0)
for
循环可以正常工作...但是请确保有一个定义的变量(在这种情况下为空数组)来保存要从迭代中推送的videoName
s。
var videoNames = function(listofVideos) {
var sourceArray = [];
for (var i = 0; i < listofVideos.length; i++) {
sourceArray.push(listofVideos[i].videoName);
}
return sourceArray;
}
从MDN中检出Array.prototype.map(),以说明这种类型的“映射”是如何工作的!