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

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)



慕尼黑8549860
浏览 102回答 2
2回答

白衣染霜花

您可以使用.map():rows_Data = rows_Data.map((entry) => ({ ...entry, file_Name: entry.file_Name.split(',') }));例子:let rows_Data =&nbsp; [{&nbsp; &nbsp; "id": 4,&nbsp; &nbsp; "user_id": 2,&nbsp; &nbsp; "description": " Hi How are you ",&nbsp; &nbsp; "post_type": 0,&nbsp; &nbsp; "created_date": "2019-01-28T19:30:49.000Z",&nbsp; &nbsp; "name": "sankar ",&nbsp; &nbsp; "mobile": 9985849966,&nbsp; &nbsp; "picture_url": "2.jpg",&nbsp; &nbsp; "post_id": 4,&nbsp; &nbsp; "saved_name": "9.jpg",&nbsp; &nbsp; "file_Name": "10.jpg,9.jpg"&nbsp;}, {&nbsp; &nbsp; "id": 3,&nbsp; &nbsp; "user_id": 1,&nbsp; &nbsp; "description": " Working a Fine ",&nbsp; &nbsp; "post_type": 0,&nbsp; &nbsp; "created_date": "2019-01-25T18:40:41.000Z",&nbsp; &nbsp; "name": "Sivasankar",&nbsp; &nbsp; "mobile": 9985849955,&nbsp; &nbsp; "picture_url": "5.jpg",&nbsp; &nbsp; "post_id": 3,&nbsp; &nbsp; "saved_name": "8.jpg",&nbsp; &nbsp; "file_Name": "7.jpg,8.jpg"&nbsp;}, {&nbsp; &nbsp; "id": 2,&nbsp; &nbsp; "user_id": 1,&nbsp; &nbsp; "description": " Hello hi",&nbsp; &nbsp; "post_type": 1,&nbsp; &nbsp; "created_date": "2019-01-21T12:51:16.000Z",&nbsp; &nbsp; "name": "Sivasankar",&nbsp; &nbsp; "mobile": 9985849955,&nbsp; &nbsp; "picture_url": "5.jpg",&nbsp; &nbsp; "post_id": 2,&nbsp; &nbsp; "saved_name": "4.jpg",&nbsp; &nbsp; "file_Name": "6.jpg,5.jpg,4.jpg"&nbsp;}, {&nbsp; &nbsp; "id": 1,&nbsp; &nbsp; "user_id": 1,&nbsp; &nbsp; "description": " Hi How are you ",&nbsp; &nbsp; "post_type": 0,&nbsp; &nbsp; "created_date": "2019-01-21T12:50:51.000Z",&nbsp; &nbsp; "name": "Sivasankar",&nbsp; &nbsp; "mobile": 9985849955,&nbsp; &nbsp; "picture_url": "5.jpg",&nbsp; &nbsp; "post_id": 1,&nbsp; &nbsp; "saved_name": "1.jpg",&nbsp; &nbsp; "file_Name": "3.jpg,2.jpg,1.jpg"&nbsp;}]rows_Data = rows_Data.map((entry) => ({ ...entry, file_Name: entry.file_Name.split(',') }));console.log(rows_Data);

陪伴而非守候

为什么不只是修改原始数组,它可以很容易地完成&nbsp; rows_Data.forEach(r =>r.file_Name = r.file_Name.split(","));&nbsp; &nbsp; &nbsp; &nbsp;或者如果您想保留原始值,只需添加一个新属性&nbsp; rows_Data.forEach(r =>r.file_NameArray = r.file_Name.split(","));&nbsp; &nbsp; &nbsp; &nbsp;rows_Data =&nbsp; [{&nbsp; &nbsp; "id": 4,&nbsp; &nbsp; "user_id": 2,&nbsp; &nbsp; "description": " Hi How are you ",&nbsp; &nbsp; "post_type": 0,&nbsp; &nbsp; "created_date": "2019-01-28T19:30:49.000Z",&nbsp; &nbsp; "name": "sankar ",&nbsp; &nbsp; "mobile": 9985849966,&nbsp; &nbsp; "picture_url": "2.jpg",&nbsp; &nbsp; "post_id": 4,&nbsp; &nbsp; "saved_name": "9.jpg",&nbsp; &nbsp; "file_Name": "10.jpg,9.jpg"&nbsp;}, {&nbsp; &nbsp; "id": 3,&nbsp; &nbsp; "user_id": 1,&nbsp; &nbsp; "description": " Working a Fine ",&nbsp; &nbsp; "post_type": 0,&nbsp; &nbsp; "created_date": "2019-01-25T18:40:41.000Z",&nbsp; &nbsp; "name": "Sivasankar",&nbsp; &nbsp; "mobile": 9985849955,&nbsp; &nbsp; "picture_url": "5.jpg",&nbsp; &nbsp; "post_id": 3,&nbsp; &nbsp; "saved_name": "8.jpg",&nbsp; &nbsp; "file_Name": "7.jpg,8.jpg"&nbsp;}, {&nbsp; &nbsp; "id": 2,&nbsp; &nbsp; "user_id": 1,&nbsp; &nbsp; "description": " Hello hi",&nbsp; &nbsp; "post_type": 1,&nbsp; &nbsp; "created_date": "2019-01-21T12:51:16.000Z",&nbsp; &nbsp; "name": "Sivasankar",&nbsp; &nbsp; "mobile": 9985849955,&nbsp; &nbsp; "picture_url": "5.jpg",&nbsp; &nbsp; "post_id": 2,&nbsp; &nbsp; "saved_name": "4.jpg",&nbsp; &nbsp; "file_Name": "6.jpg,5.jpg,4.jpg"&nbsp;}, {&nbsp; &nbsp; "id": 1,&nbsp; &nbsp; "user_id": 1,&nbsp; &nbsp; "description": " Hi How are you ",&nbsp; &nbsp; "post_type": 0,&nbsp; &nbsp; "created_date": "2019-01-21T12:50:51.000Z",&nbsp; &nbsp; "name": "Sivasankar",&nbsp; &nbsp; "mobile": 9985849955,&nbsp; &nbsp; "picture_url": "5.jpg",&nbsp; &nbsp; "post_id": 1,&nbsp; &nbsp; "saved_name": "1.jpg",&nbsp; &nbsp; "file_Name": "3.jpg,2.jpg,1.jpg"&nbsp;}]console.log(rows_Data);rows_Data.forEach(r =>r.file_Name = r.file_Name.split(","));&nbsp; &nbsp; &nbsp; &nbsp;console.log(rows_Data)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript