将数组转换为批量使用Sequelize在nodejs中创建

时间:2017-11-28 11:32:01

标签: mysql angularjs node.js

这是来自angularjs控制器的req.body json数据:

{ 
    phoneno: [ 
               { id: 1, gsm: '123457801',  firstName: 'Mohamed',  lastName: 'Sameer'},
               { id: 2, gsm: '123450987',  firstName: 'Hameed',   lastName: 'Basha' } 
             ],
    sender: 'ifelse',
    message: 'Hello Test' 

}

我想从req.body

获取gsm值

我想将上述结构改为此类型:

[{phoneno:123457801;sender:'ifelse';message:'Hello Test'},{phoneno:123450987;sender:ifelse;message:'Hello Test'}]

然后我才能插入mysql。

1 个答案:

答案 0 :(得分:3)

我认为这对你有帮助!...



var body={ 
    phoneno: [ 
               { id: 1, gsm: '123457801',  firstName: 'Mohamed',  lastName: 'Sameer'},
               { id: 2, gsm: '123450987',  firstName: 'Hameed',   lastName: 'Basha' } 
             ],
    sender: 'ifelse',
    message: 'Hello Test' 

};
var new_req_body=[];
for(var i=0;i<body.phoneno.length;i++){

var new_arr={
            sender:body.sender,
            message:body.message,
            phoneno:body.phoneno[i].gsm
          };
new_req_body.push(new_arr);
}
console.log(new_req_body);
&#13;
&#13;
&#13;