Utilizing the node.js API, I have retrieved data from the database in a nested JSON string format. The "file_Name" field currently displays as "3.jpg, 2.jpg, 1.jpg", but my goal is to have it displayed as an array like this: "file_Name": ["3.jpg", "2.jpg", "1.jpg"]. I attempted to achieve this by using a for loop and push method; however, though I managed to extract the array from rows_Data using the for loop, I am unsure how to push it back into rows_Data.
If anyone could review the code and suggest an alternative solution, I would greatly appreciate it.
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)
Desired Output:
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"]
}]