我在json对象下面有这个
{
"phoneno": [
{
"field1": "Mohamed",
"field2": "123456789",
"field3": "Sameer"
},
{
"field1": "Ganesh",
"field2": "987654321",
"field3": "Pandiyan"
}
],
"sender": "ifelse",
"message": "test"
}
我在UI中使用ANGULARJS ng-repeat显示这个JSON,我也动态显示单选按钮,见下图
看到有单选按钮检查,在这列中,我有两个手机号码,
所以我想制作这样的最终对象:
[
{"phoneno" : 123456789 ; "sender" : "ifelse" ; "message" : "test"},
{"phoneno" : 987654321 ; "sender" : "ifelse" ; "message" : "test"}
]
例如,如果我有这种类型的JSON:
{
"phoneno": [
{
"field1": "123456789",
"field2": "Mohamed",
"field3": "Sameer"
},
{
"field1": "987654321",
"field2": "Ganesh",
"field3": "Pandiyan"
}
],
"sender": "ifelse",
"message": "test"
}
现在又来了
我使用ng-repeat在UI中显示这个JSON,我也动态显示单选按钮,请参见下图
看到有单选按钮选中,在这列中,我有两个手机号码,
所以我想制作这样的最终对象:
[
{"phoneno" : 123456789 ; "sender" : "ifelse" ; "message" : "test"},
{"phoneno" : 987654321 ; "sender" : "ifelse" ; "message" : "test"}
]
如果我有这种类型的对象,我可以在sequelize的帮助下批量插入到mysql中.
怎么做?
我的api.js(我还没编码,寻找解决方案)
exports.sendFile2Sms = function (req, res) {
};
最佳答案 它对您有所帮助,在您的数据中,您应该提到选择了哪个列,例如选择了field2意味着您应该将其附加到您的JSON数据.
在你的情况下,我添加了新的密钥来找到哪个是“selected_radio”列:“field2”,你应该在你的前端做同样的事情
这是更改JSON数据的角度部分
var data={
"phoneno": [
{
"field1": "Mohamed",
"field2": "123456789",
"field3": "Sameer"
},
{
"field1": "Ganesh",
"field2": "987654321",
"field3": "Pandiyan"
}
],
"sender": "ifelse",
"message": "test",
"selected_radio":"field2"
};
var new_data=[];
_.each(data.phoneno,function(obj){
new_data.push({"phoneno":obj[data.selected_radio],"sender":data.sender,"message":data.message});
})
console.log(new_data);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>