如何在Javascript中制作json数据?

时间:2018-05-03 16:02:42

标签: javascript json

我有一个类似下面的数组:

[
        {
            name: "ReceiverId",
            value: "1"
        },
        {name: "TransSequence", value: "KPJ7dL2pmx0njInNRAzUug"},
        {
            name: "email-address",
            value: "xcv@cvb.com"
        },
        {
            name: "mobile-number",
            value: "9321313213"
        },
        {
            name: "ReceiverId",
            value: "2"
        },
        {
            name: "TransSequence",
            value: "KPJ7dL2pmx0njInNRAzUug"
        },
        {
            name: "email-address",
            value: "xcv@cvb.com"
        }


    ]

现在我想让我的json数据看起来像这样:

{
    "MainData": [
      {
      "TransSequence": "wpiuVJw",
        "ReceiverId": "1",    
        "ReceiverEmail": "xcv@cvb.com",
        "ReceiverMobileNo": "9321313213",
      },
      {  
        "TransSequence": "xowpiuVJw",
        "ReceiverId": "2",    
        "ReceiverEmail": "xcv@cvb.com",
        "ReceiverMobileNo": "9321313213",
      }
    ]
    }

但是我尝试了下面的代码片段:

mArr = []
obj = {};
for(var i=0; i<mainArr.length; i++){

//  obj = {};
    for(j = 0; j < 4; j++){
        obj[mainArr[i].name] = mainArr[i].value
    }
}

但是上面的代码段仅返回最后一个值。但是,我也尝试转换为基于字符串的json,但在最后一个数组中,它返回为,,在解析时会抛出错误。

提供帮助可以获得赞赏!!

3 个答案:

答案 0 :(得分:2)

根据您尝试的代码,您使用的是js。

在js中,您可以使用reduce来汇总数组。使用Object.assign组合对象。

let arr = [{"name":"ReceiverId","value":"1"},{"name":"TransSequence","value":"KPJ7dL2pmx0njInNRAzUug"},{"name":"email-address","value":"xcv@cvb.com"},{"name":"mobile-number","value":"9321313213"},{"name":"ReceiverId","value":"2"},{"name":"TransSequence","value":"KPJ7dL2pmx0njInNRAzUug"},{"name":"email-address","value":"xcv@cvb.com"},{"name":"mobile-number","value":"9321231321"},{"name":"ReceiverId","value":"1"},{"name":"TransSequence","value":"KPJ7dL2pmx0njInNRAzUug"},{"name":"email-address","value":"xcv@cvb.com"},{"name":"mobile-number","value":"9321231321"},{"name":"ReceiverId","value":"2"}];


let propertyCount = 4;
let result = arr.reduce((c, v, i) => {
  let o = Math.floor(i / propertyCount);
  c[o] = c[o] || {};
  c[o] = Object.assign(c[o], {[v.name]: v.value});
  return c;
}, []);

console.log(result);

答案 1 :(得分:0)

这是正确的代码

&#13;
&#13;
let mainArr = [{"name":"ReceiverId","value":"1"},{"name":"TransSequence","value":"KPJ7dL2pmx0njInNRAzUug"},{"name":"email-address","value":"xcv@cvb.com"},{"name":"mobile-number","value":"9321313213"},{"name":"ReceiverId","value":"2"},{"name":"TransSequence","value":"KPJ7dL2pmx0njInNRAzUug"},{"name":"email-address","value":"xcv@cvb.com"},{"name":"mobile-number","value":"9321231321"},{"name":"ReceiverId","value":"1"},{"name":"TransSequence","value":"KPJ7dL2pmx0njInNRAzUug"},{"name":"email-address","value":"xcv@cvb.com"},{"name":"mobile-number","value":"9321231321"}];

const obj = {
    MainData: [],
}
for(var i=0; i<mainArr.length / 4; i++){

    const local = {}
    for(j = i * 4; j < i * 4 + 4; j++){
        local[mainArr[j].name] = mainArr[j].value
    }
    obj.MainData.push(local);
}

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

答案 2 :(得分:0)

这是我的看法:

time[1]
const input = [
    {name: "ReceiverId", value: "1"},
    {name: "TransSequence", value: "KPJ7dL2pmx0njInNRAzUug"},
    {name: "email-address", value: "xcv@cvb.com"},
    {name: "mobile-number", value: "9321313213"},
    {name: "ReceiverId", value: "2"},
    {name: "TransSequence", value: "KPJ7dL2pmx0njInNRAzUug"},
    {name: "email-address", value: "xcv@cvb.com"},
    {name: "mobile-number", value: "9321231321"},
    {name: "ReceiverId", value: "1"},
    {name: "TransSequence", value: "KPJ7dL2pmx0njInNRAzUug"},
    {name: "email-address", value: "xcv@cvb.com"},
    {name: "mobile-number", value: "9321231321"},
    {name: "ReceiverId", value: "2"}
];

let data = [];
input.forEach(e => {
    // If it's a new data point, add it.
    if (e.name === 'ReceiverId') {
        data.push({
            ReceiverId: e.value
        })
    // Add it to the last array.
    } else {
        data[data.length-1][e.name] = e.value
    }
})

let output = {
    MainData: data
}

// Your output
console.log(output)