将ajax响应转换为特定格式?

时间:2017-08-18 12:30:13

标签: javascript php ajax

enter image description here输入:var result= JSON.parse(response); console.log(result);

以下是console.log(result)的输出;现在我想将此转换为下面的输出,我希望这样。

Output:     
  {day: Array(7), amount: Array(7)}
  amount: (7) [413, 14, 2, 37, 50, 22, 82]
  day: (7) ["02-Jan-16", "03-Jan-16", "04-Jan-16", "05-Jan-16", "06-Jan-16", 
  "07-Jan-16", "08-Jan-16"]


 This input is ajax response data. Now i  want to convert this response data 
 into a particular format which i describe below. My output should look like 
 this. How can i do this?


 Output I want:

 [[02-Jan-16,413],[03-Jan-16,14],[04-Jan-16,2],[05-Jan-16,37], [06-Jan-
 16,50],[07-Jan-16,22],[08-Jan-16,82]]

enter image description here 这是输出图像。看看吧。

2 个答案:

答案 0 :(得分:1)

首先,您必须决定使用哪种语言进行json编码PHP或JavaScript?这两种语言都有内置函数,首先你应该熟悉嵌套数组,一旦你理解了数组构造,那么使用内置函数形成预期的JSON格式只需要几分钟的工作。

如果网上已有大量信息,请不要指望我们提供代码,只需谷歌即可。

答案 1 :(得分:0)

您可以为新数组映射所需的值。



function getData(object) {
    return data.amount.map(function(a, i) {
        return [object.day[i], a];
    });
}

var data = {
        amount: [413, 14, 2, 37, 50, 22, 82],
        day: ["02-Jan-16", "03-Jan-16", "04-Jan-16", "05-Jan-16", "06-Jan-16", "07-Jan-16", "08-Jan-16"]
    },
    result = getData(data);
  
console.log(result);

.as-console-wrapper { max-height: 100% !important; top: 0; }