简体   繁体   English

在发送到服务器之前在 JS 中准备对象?

[英]Object preparation in JS before sending to server?

I'm trying to prepare an object to POST to my server to store some information.我正在尝试准备一个对象以 POST 到我的服务器以存储一些信息。 This object requires me to do a few GET requests depending on how the user chooses to gather all the information needed to POST.这个对象需要我做一些 GET 请求,这取决于用户如何选择收集 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.我意识到我必须修改对象才能真正将它们放入 JSON 中的正确值对中,我不确定是否有更好的方法来做到这一点。

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.我只是以一种简单的方式展示了这一点,但实际情况有 6-7 个很长的对象,它们都需要修改并放入一个 JSON 中。 The server API is written this way to accept input, and I don't have any say in it.服务器 API 就是这样编写的,用于接受输入,我对此没有任何发言权。

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.我觉得我缺少一些技巧。

You could use ES6 object destructuring .您可以使用 ES6对象解构


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
}

It sounds like you need something like JQuery's or Angular's extend() function, but with a twist for sub-mapping the keys.听起来您需要类似 JQuery 或 Angular 的 extend() 函数,但需要对键进行子映射。

https://api.jquery.com/jquery.extend/ 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.例如,请注意合并对象中的“id”是 2,而不是 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"
// }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 节点js在发送之前从对象中删除敏感数据 - Node js remove sensitive data from object before sending it 在下一个 js 环境中发送到服务器之前如何处理数据? - How to process data before sending to server in next js environment? 从客户端JS,服务器Nodejs发送JSON作为[对象对象]读取 - Sending JSON from Client JS, Server Nodejs Reads as [object object] 将javascript对象发送到服务器 - Sending a javascript object to a server 执行应用程序 Plesk NodeJS 前的准备工作 - Preparation work before executing the app Plesk NodeJS 我们可以在发送响应之前停止由Node.js中的http发布请求进行的正在进行的Server进程吗? - Can we stop ongoing Server process made by http post request in Node.js before sending a response? 如何在发送到服务器之前将 mimetype (video/webm) 和编解码器 (vp9) 添加到 blob object - How to add mimetype (video/webm) and codec (vp9) to the blob object before sending to server 文件数据在发送到服务器时显示为空 object,但在通过 axios api 发送之前包含数据 - File data shows empty object when sent to server but contains data before sending via axios api 在发送到服务器之前,如何生成POST数组作为客户端/ JavaScript对象? - How can I generate the POST array as a clientside / JavaScript object before sending to server? 保存Node Js之前发送数据 - Sending data before saving Node Js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM