Slodin
Slodin

Reputation: 89

Object preparation in JS before sending to server?

I'm trying to prepare an object to POST to my server to store some information. This object requires me to do a few GET requests depending on how the user chooses to gather all the information needed to POST. I realized I have to modify the object to actually get them into the correct value pairs in JSON, and I'm not sure if there is a better way to do it.

I'm only showing this in a simple way, but the actual matter has 6-7 very long objects, and they all needs to be modified and fit in one JSON. The server API is written this way to accept input, and I don't have any say in it.

For example: What I get back from requests

object1: {
 id: 1,
 name: "table",
 price: 3499
}

object2: {
 id: 5,
 lat: 48.56,
 lng: -93.45,
 address: "1080 JavaScript Street"
}

What I need it to become:

data: {
 map_id: 5,
 product_id: [1],
 product_name: ["table"],
 product_price: [3499],
 start_lat: 48.56,
 start_lng: -93.45,
 start_address: "1080 JavaScript Street"
}

So far I just do the dumb way to just stitch them together, I just wrote this on here so it doesn't work, but should show logically what I'm thinking:

prepareDataToSend = (object1, object2) => {
 //exclude uninit handling, and newObject init for arrays
 let newObject = {};
 newObject.map_id = object2.id;
 //if there are more of object1 then I have to loop it
 newObject.product_id.push(object1.id);
 newObject.product_name.push(object1.name);
 ...etc
}

I do get the result I'm looking for, but this feels really ineffective and dumb.Not to mention this seems very unmaintainable. Is there a better way to do this? I feel like there is some techniques i'm missing.

Upvotes: 1

Views: 218

Answers (2)

Kieron Alsmith
Kieron Alsmith

Reputation: 63

It sounds like you need something like JQuery's or Angular's extend() function, but with a twist for sub-mapping the keys.

https://api.jquery.com/jquery.extend/

Here's a simple version of it, tweaked for your needs.

//merge the N objects.  Must have a "prefix" property to configure the new keys
var extendWithKeyPrefix = function() {
  if (arguments.length == 0) return; //null check
  var push = function(dst, arg) {
    if (typeof arg != 'undefined' && arg != null) { //null check
      var prefix = arg["prefix"]; //grab the prefix
      if (typeof prefix != 'undefined' && prefix != null) { //null check
        for (var k in arg) { //add everything except for "prefix"
          if (k != "prefix") dst[prefix+k] = arg[k]; 
        }
      }
    }
    return dst;
  }
  arguments.reduce(push);
}

Please note that the value of the last object that uses a particular key will win. For example notice that "id" in the merged object is 2, rather than 1.

var object1 = {id: 1, unique1: "One", prefix: "product_"};
var object2 = {id: 2, unique2: "Two", prefix: "product_"};
var object3 = {id: 3, unique3: "Three", prefix: "office_"};
var merged = {};

extend(merged, object1, object2);

// value of merged is...
// { product_id: 2, 
//   product_unique1: "One", 
//   product_unique2: "Two", 
//   office_id: 3, 
//   office_unique3: "Three"
// }

Upvotes: 0

Khalid Ali
Khalid Ali

Reputation: 1224

You could use ES6 object destructuring.


let object1 = {
 id: 1,
 name: "table",
 price: 3499
};

let object2 = {
 id: 5,
 lat: 48.56,
 lng: -93.45,
 address: "1080 JavaScript Street"
};

// declaring the new object with the new properties names.
let newObject = {
  map_id: '',
  product_id: [],
  product_name: [],
  product_price: [],
  start_lat: '',
  start_lng: '',
  start_address: ''
};

// destructuring "object1"
({id: newObject.product_id[0],
  name: newObject.product_name[0],
  price: newObject.product_price[0]} = object1);

// destructuring "object2"
({id: newObject.map_id,
  lat: newObject.start_lat,
  lng: newObject.start_lng,
  address: newObject.start_address} = object2);

console.log(newObject)

Result:

{
  map_id: 5,
  product_id: [1],
  product_name: ["table"],
  product_price: [3499],
  start_address: "1080 JavaScript Street",
  start_lat: 48.56,
  start_lng: -93.45
}

Upvotes: 1

Related Questions