SIDDHARTH VARSHNEY
SIDDHARTH VARSHNEY

Reputation: 659

Posting data by combining the data from a form and a fetch api response

Ok so basically I want to combine two data in a particular format and the make a post request. The response I am getting from my fetch API :

{
    "id":"some-id" 
}

JSON formate of my form data:

{
   "origin_port":"jnasjd",
   "destination_port":"csdcsa",
   "stops":3,
   "departure":"kanlskd",
   "arrival":"sndclk",
   "update_params":[
      {
         "stops":3,
         "departure":"jasnkc",
         "arrival":"jnacksa"
      },
      {
         "stops":3,
         "departure":"hsdbkjcs",
         "arrival":"xsnkjc"
      }
   ]
}

Required :

{  "id":"some-id"
   "origin_port":"jnasjd",
   "destination_port":"csdcsa",
   "stops":3,
   "departure":"kanlskd",
   "arrival":"sndclk",
   "update_params":[
      {
         "id":"some-id"
         "stops":3,
         "departure":"jasnkc",
         "arrival":"jnacksa"
      },
      {
         "id":"some-id"
         "stops":3,
         "departure":"hsdbkjcs",
         "arrival":"xsnkjc"
      }
   ]
}

My update_params dynamic array and I want to add id in its every object I am using react framework as frontend library.

Upvotes: 1

Views: 599

Answers (2)

Fatih Aktaş
Fatih Aktaş

Reputation: 1564

Simple: You can use Object.assign(obj1, obj2) to combine the objects.

var combined = Object.assign(obj1, obj2);

REF: Object.assign()

Harder: I played around your JSON to create the sample JavaScript objects. Below, I am going to show you how to combine the properties of multiple JSON objects.

We will combine the properties of multiple JSON objects in a new combined object.

var obj1 = JSON.parse(`{ // Creating a sample object you get from the fetch API
  "id":"some-id" 
}`);
var obj2 = JSON.parse(`{ // Creating a sample object for your form data
  "origin_port":"jnasjd",
  "destination_port":"csdcsa",
  "stops":3,
  "departure":"kanlskd",
  "arrival":"sndclk",
  "update_params":[
     {
        "stops":3,
        "departure":"jasnkc",
        "arrival":"jnacksa"
     },
     {
        "stops":3,
        "departure":"hsdbkjcs",
        "arrival":"xsnkjc"
     }
  ]
}`);

// This will be the object made up of the properties of the two objects above obj1 and obj2
var combined = {};

for(var prop in obj1){ // Looping through the properties of obj1
  combined[prop] = obj1[prop]; // Setting the prop of obj1 to be the prop of combined
}
for(var prop in obj2){
  combined[prop] = obj2[prop]; // Setting the prop of obj2 to be the prop of combined
}

document.getElementById("app").innerHTML = JSON.stringify(combined, null, 2);

Upvotes: 0

Mario
Mario

Reputation: 4998

You could append the id to the main object, and for the dynamic update_params array you could use map to append the id to each object in the array

Please take a look to this example

const id = {
    "id":"some-id" 
};

const dataset = {
   "origin_port":"jnasjd",
   "destination_port":"csdcsa",
   "stops":3,
   "departure":"kanlskd",
   "arrival":"sndclk",
   "update_params":[
      {
         "stops":3,
         "departure":"jasnkc",
         "arrival":"jnacksa"
      },
      {
         "stops":3,
         "departure":"hsdbkjcs",
         "arrival":"xsnkjc"
      }
   ]
};

dataset.id = "some-id";
dataset.update_params.map(params => params.id = "some-id");

console.log(dataset)

Upvotes: 1

Related Questions