表单来自Web服务响应的数据集值

时间:2018-02-05 18:02:50

标签: javascript ajax

我正在尝试从webservice响应中设置表值。 我得到了这样的网络服务响应。但是,如果他的回答是这样意味着它没有被设置在那里。这种反应可能是动态的。

0:{name: "image.png", base64: "iVBORw"}
1:{name: "download.png", base64: "iVBO"}
2:{name: "test-animation.gif", base64: "R0lGODlhLAEs"}

如何将其更改为??

[["image.png", "iVBORw"],["download.png", "iVBO"],[test-animation.gif", "R0lGODlhLAEs"]]

这是我正在尝试的东西

$.cordys.ajax({
					method: "somewebservice",
					namespace: "Package",
					parameters: {
						emailid:mailidvalue
					},
					dataType: '* json',
					 success: function (result) { 
					 output=result;
					 bodycontent=output["data"]["body"];
 var attachvalue=result.data.tuple;

			 $('#attachmenttable').DataTable( {
        "data": attachvalue,
        columns: [
            { title: "File Name" },
            { title: "Base64" }
        ]
    } );
                       },
					error: function(err){
						console.log(err);
					}
					});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

1 个答案:

答案 0 :(得分:0)

我看到你给出的内容和你想要的内容之间的1-1映射,因此使用Array.prototype.map()$.map

&#13;
&#13;
var given = {
  0: {
    name: "image.png",
    base64: "iVBORw"
  },
  1: {
    name: "download.png",
    base64: "iVBO"
  },
  2: {
    name: "test-animation.gif",
    base64: "R0lGODlhLAEs"
  }
};

var desired = Object.keys(given).map(function(key) {
  return [ given[key].name, given[key].base64 ];
});

console.log(desired);
&#13;
&#13;
&#13;

&#13;
&#13;
var given = {
  0: {
    name: "image.png",
    base64: "iVBORw"
  },
  1: {
    name: "download.png",
    base64: "iVBO"
  },
  2: {
    name: "test-animation.gif",
    base64: "R0lGODlhLAEs"
  }
};

var desired = $.map(given, function (value, key) {
  return [[ given[key].name, given[key].base64 ]];
});

console.log(desired);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;