在javascript中用数组填充对象

Fill the object with array in javascript

本文关键字:填充 对象 数组 javascript      更新时间:2023-09-26

我在javascript中有模型,除了其中一个之外,我用值填充这个模型。

var files = $("#fileUpload").get(0).files[0];
    // Add the uploaded image content to the form data collection
    if (files.length > 0) {
        data.append("UploadedImage", files[0]);
    }
    var ResturantSharingViewModel =
   {
       Type: $("#SharingTargetType").val(),
       SharingTitle: $("#SharingTitle").val(),
       content: $("#Content").val(),
       ItemId : $("#ItemId").val(),
       Photos: files[0]
   }

模型中的照片参数未定义。

我想用文件数组填充照片对象

但我不知道该怎么做?

我认为您正在寻找整个文件数组,而不是第一个索引

var files = $("#fileUpload").get(0).files; //remove the index [0]
    // Add the uploaded image content to the form data collection
    if (files.length > 0) {
        data.append("UploadedImage", files[0]);
    }
    var ResturantSharingViewModel =
   {
       Type: $("#SharingTargetType").val(),
       SharingTitle: $("#SharingTitle").val(),
       content: $("#Content").val(),
       ItemId : $("#ItemId").val(),
       Photos: files[0]
   }