How can we divide the nested JSON string in Node.js and append it to the existing JSON object?

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"]
 }]

Answer №1

You can utilize the .map() method:

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

For instance:

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);

Answer №2

Why not simply adjust the original array? It's a simple task with

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

Alternatively, if you prefer to retain the original value, just introduce a new property

  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)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ways to conceal a div element when the JSON data does not contain a value

If the value in "desc" is empty, then hide <div24> and <popup-desc>. html <div class="popup"> <div class="popup-top" style="background-color: '+e.features[0].properties.fill+';"> ...

Using VueJS to dynamically manipulate URL parameters with v-model

Hello, I am new to coding. I am working on calling an API where I need to adjust parts of the querystring for different results. To explain briefly: <template> <div> <input type="text" v-model="param" /> ...

Electron: Interactive menu customization

Is there a way in Electron to dynamically enable/disable specific MenuItem in the context menu based on the element that the user right-clicks on? Additionally, I am looking for a method to identify the exact element clicked and pass that information to th ...

The mobile screen size shortcuts are malfunctioning, while the regular links are functioning properly

ISSUE: Links in alias buttons are not working on mobile screen size, while normal links to other webpages like social media work fine. Description I created a project using an HTML, CSS, JS building tool from The project was exported and placed into a ...

Broadcast signals to an overarching frame

I have successfully embedded a chatbot (Angular 14 app) in an iframe and now I need to determine whether the frame should be minimized so it can fit within the parent container. My goal is to send custom events to the receiving frame. let iframeCanvas = do ...

Do we always need to use eval() when parsing JSON objects?

<!DOCTYPE html> <html> <body> <h2>Creating a JSON Object in JavaScript</h2> <p> Name: <span id="jname"></span><br /> Evaluated Name: <span id="evalname"></span><br /> <p> <s ...

Tally each div individually and display the count within each div, instead of showing the total count across

I have come across various solutions that show the total number of certain special divs, such as: $('.someclass').length However, I am facing a different challenge. I want to sequentially count each div with a numerical sequence. For instance, ...

An issue arises in the NodeJS Express authentication process when attempting to set headers after they have already been sent,

Currently, I am working on developing a basic authentication system using the POST method. app.post("/api/auth",function(req,resp) { var username = req.body.username||req.param('username'); var password = req.body.password||req.param(&a ...

Interactive material design drop-down menu

Currently, I am working on a dynamic drop-down menu that utilizes material-ui js. However, I have encountered an issue where clicking on one menu opens all the menus simultaneously, and vice versa when trying to close them. If you would like to view the c ...

Creating a cascading select box with two levels in PHP and MySQLExplanation on how to generate a two-tier connected

While I have successfully retrieved values from a MySQL database using a select box in PHP, I am struggling with implementing a two-level chained select box. Does anyone have any sample code or suggestions on how to achieve this? Thank you. ...

Can you explain the significance of polyfills in HTML5?

What exactly are polyfills in the context of HTML5? I've come across this term on multiple websites discussing HTML5, such as HTML5-Cross-Browser-Polyfills. Essentially, polyfills refer to tools like shims and fallbacks that help add HTML5 features ...

problem | JQuery slide causing automatic page scroll to top

Here is the HTML for my slide: <div class="slide_container"> <div class="one_slide"> <a href="#"><img alt="slide_img" class="slide_img img-responsive" src="images/slide2.jpg"></a> </div> ...

What sets apart the CSS file directories in a React application compared to those in an Express server?

For instance, there is a public folder that contains all the css files, and a separate view folder for ejs files. When linking the css file in the ejs file, the code usually looks like this: <link rel=”stylesheet” href=”styles.css”> or like ...

Typescript: Determine when a property should be included depending on the value of another property

Having some difficulty with Typescript and React. Specifically, I am trying to enforce a type requirement for the interface Car where the property colorId is only required if the carColor is set to 'blue'. Otherwise, it should not be included in ...

Do you need the Tooltip Module for Angular-bootstrap popover to work properly?

"Could it be possible that my popover isn't working because it requires the tooltip module just like the Bootstrap jQuery plugin?" Is this the reason my popover isn't functioning as expected? What exactly is the tooltip module and do I need to i ...

Implementing a JSON query with an onkeyup event listener

My main objective with this code is to trigger a json query request by using a textfield to specify the location. It's essentially a quick search feature that searches for specific content on a different website. For example, if I input www.calsolum/ ...

Do not generate an HTML cookie

I've encountered an issue while trying to create a cookie using the following code: window.onload = function() { var value = readCookie('username'); if(value == null){ alert("null"); document.cookie = "username=Bob; expires=Thu, 18 ...

Troubleshooting Texture Compatibility Issue with ThreeJS ShaderMaterial on iOS Devices

Seeking assistance with shaders in Threejs. I have a plane that requires a mixture of 10 different textures; currently using ShaderMaterial and passing all textures for blending. Below is my Fragment Shader code: vec3 CFull = texture2D(tFull, vUv).rgb; vec ...

What steps can we take to perform queries within the context of a specific element in a Jest test?

Currently, I am in the process of writing Jest tests for a React application. Imagine that there is a webpage with multiple instances of a specific element present. For instance, let's consider a scenario where there are two buttons on the page. My ob ...

Integrating information from an API into the Document Object Model

const url = `https://catfact.ninja/fact?max_length=140`; const getFact = () => { return fetch('https://catfact.ninja/fact?max_length=140') .then(res => res.json()) } const createFactDiv = (fact) => { const factContainer = documen ...