为了账号安全,请及时绑定邮箱和手机立即绑定

node.js rows_data 如何拆分嵌套的json字符串并将其推送到同一个json对象

node.js rows_data 如何拆分嵌套的json字符串并将其推送到同一个json对象

慕尼黑8549860 2022-01-07 10:42:49
NATIVE with node.js API 从数据库中获取数据,我以嵌套的 JSON 字符串格式获取数据,主要是“file_Name”:“3.jpg,2.jpg,1.jpg”,但我想要“file_Name”:[ “3.jpg”、“2.jpg”、“1.jpg”] 嵌套 JSON 数组格式,我尝试使用 for 循环和推送,通过使用 for 循环我从 rows_Data 获取数组但我不知道 w将其推送到 rows_Data。如果有人知道任何替代解决方案,请检查代码,请帮助我。,rows_Data =  [{    "id": 4,    "user_id": 2,    "description": " Hi How are you ",    "post_type": 0,    "created_date": "2019-01-28T19:30:49.000Z",    "name": "sankar ",    "mobile": 9985849966,    "picture_url": "2.jpg",    "post_id": 4,    "saved_name": "9.jpg",    "file_Name": "10.jpg,9.jpg" }, {    "id": 3,    "user_id": 1,    "description": " Working a Fine ",    "post_type": 0,    "created_date": "2019-01-25T18:40:41.000Z",    "name": "Sivasankar",    "mobile": 9985849955,    "picture_url": "5.jpg",    "post_id": 3,    "saved_name": "8.jpg",    "file_Name": "7.jpg,8.jpg" }, {    "id": 2,    "user_id": 1,    "description": " Hello hi",    "post_type": 1,    "created_date": "2019-01-21T12:51:16.000Z",    "name": "Sivasankar",    "mobile": 9985849955,    "picture_url": "5.jpg",    "post_id": 2,    "saved_name": "4.jpg",    "file_Name": "6.jpg,5.jpg,4.jpg" }, {    "id": 1,    "user_id": 1,    "description": " Hi How are you ",    "post_type": 0,    "created_date": "2019-01-21T12:50:51.000Z",    "name": "Sivasankar",    "mobile": 9985849955,    "picture_url": "5.jpg",    "post_id": 1,    "saved_name": "1.jpg",    "file_Name": "3.jpg,2.jpg,1.jpg" }]console.log(rows_Data);var copy= []; ; var res_ = {};var file_Name ={};var result = {};for (let i = 0; i < rows_Data.length; i++) {     copy.push(rows_Data[i].file_Name);     for (let j = 0; j < copy.length; j++) {          res_[j] = copy[j].split(",");                     for (let k = 0; k < res_.length; j++) {               rows_Data[k].file_Name.push(res_[k]);           }       }  }console.log(copy); console.log( res_);console.log(rows_Data)
查看完整描述

2 回答

?
白衣染霜花

TA贡献1796条经验 获得超10个赞

您可以使用.map():


rows_Data = rows_Data.map((entry) => ({ ...entry, file_Name: entry.file_Name.split(',') }));

例子:


let rows_Data =  [{

    "id": 4,

    "user_id": 2,

    "description": " Hi How are you ",

    "post_type": 0,

    "created_date": "2019-01-28T19:30:49.000Z",

    "name": "sankar ",

    "mobile": 9985849966,

    "picture_url": "2.jpg",

    "post_id": 4,

    "saved_name": "9.jpg",

    "file_Name": "10.jpg,9.jpg"

 }, {

    "id": 3,

    "user_id": 1,

    "description": " Working a Fine ",

    "post_type": 0,

    "created_date": "2019-01-25T18:40:41.000Z",

    "name": "Sivasankar",

    "mobile": 9985849955,

    "picture_url": "5.jpg",

    "post_id": 3,

    "saved_name": "8.jpg",

    "file_Name": "7.jpg,8.jpg"

 }, {

    "id": 2,

    "user_id": 1,

    "description": " Hello hi",

    "post_type": 1,

    "created_date": "2019-01-21T12:51:16.000Z",

    "name": "Sivasankar",

    "mobile": 9985849955,

    "picture_url": "5.jpg",

    "post_id": 2,

    "saved_name": "4.jpg",

    "file_Name": "6.jpg,5.jpg,4.jpg"

 }, {

    "id": 1,

    "user_id": 1,

    "description": " Hi How are you ",

    "post_type": 0,

    "created_date": "2019-01-21T12:50:51.000Z",

    "name": "Sivasankar",

    "mobile": 9985849955,

    "picture_url": "5.jpg",

    "post_id": 1,

    "saved_name": "1.jpg",

    "file_Name": "3.jpg,2.jpg,1.jpg"

 }]



rows_Data = rows_Data.map((entry) => ({ ...entry, file_Name: entry.file_Name.split(',') }));


console.log(rows_Data);


查看完整回答
反对 回复 2022-01-07
?
陪伴而非守候

TA贡献1757条经验 获得超8个赞

为什么不只是修改原始数组,它可以很容易地完成


  rows_Data.forEach(r =>r.file_Name = r.file_Name.split(","));       

或者如果您想保留原始值,只需添加一个新属性


  rows_Data.forEach(r =>r.file_NameArray = r.file_Name.split(","));       

rows_Data =  [{

    "id": 4,

    "user_id": 2,

    "description": " Hi How are you ",

    "post_type": 0,

    "created_date": "2019-01-28T19:30:49.000Z",

    "name": "sankar ",

    "mobile": 9985849966,

    "picture_url": "2.jpg",

    "post_id": 4,

    "saved_name": "9.jpg",

    "file_Name": "10.jpg,9.jpg"

 }, {

    "id": 3,

    "user_id": 1,

    "description": " Working a Fine ",

    "post_type": 0,

    "created_date": "2019-01-25T18:40:41.000Z",

    "name": "Sivasankar",

    "mobile": 9985849955,

    "picture_url": "5.jpg",

    "post_id": 3,

    "saved_name": "8.jpg",

    "file_Name": "7.jpg,8.jpg"

 }, {

    "id": 2,

    "user_id": 1,

    "description": " Hello hi",

    "post_type": 1,

    "created_date": "2019-01-21T12:51:16.000Z",

    "name": "Sivasankar",

    "mobile": 9985849955,

    "picture_url": "5.jpg",

    "post_id": 2,

    "saved_name": "4.jpg",

    "file_Name": "6.jpg,5.jpg,4.jpg"

 }, {

    "id": 1,

    "user_id": 1,

    "description": " Hi How are you ",

    "post_type": 0,

    "created_date": "2019-01-21T12:50:51.000Z",

    "name": "Sivasankar",

    "mobile": 9985849955,

    "picture_url": "5.jpg",

    "post_id": 1,

    "saved_name": "1.jpg",

    "file_Name": "3.jpg,2.jpg,1.jpg"

 }]


console.log(rows_Data);


rows_Data.forEach(r =>r.file_Name = r.file_Name.split(","));       


console.log(rows_Data)


查看完整回答
反对 回复 2022-01-07
  • 2 回答
  • 0 关注
  • 101 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信