【问题标题】:How to convert array of objects to serialised?如何将对象数组转换为序列化?
【发布时间】:2018-10-13 15:24:04
【问题描述】:

我有一个对象数组

const parameters = [
  {token: '78fe6df3f'},
  {id: '12345'},
  { price: '0 - 9,000,000' },
  { 'area[]': 'Applehead Island' },
  { 'waterfront_type[]': 'Open Water' },
  { property_type_single: 'Single Family/Site Built' },
  { bedrooms: '0 - 5' },
  { baths: '0 - 5' },
  { sqft: '0 - 7500' }
];

我希望这个对象变成如下所示

https://www.example.com/properties.php?token=78fe6df3f&id=12345&price=$0%20-%20$3,480,000&area[]=Applehead%20Island&waterfront_type[]=Open%20Water&property_type_single=Single%20Family/Site%20Built&bedrooms=0%20-%205&baths=0%20-%205&sqft=0%20-%207500

请帮忙看看如何获​​得这个?

【问题讨论】:

  • 这是什么语言的?这是 JavaScript 吗?
  • 即对象数组。我需要使用它来获取调用。
  • 但是这个数组是用什么语言定义的?
  • 我不明白你的问题,恐怕。它是javascript
  • 谢谢,这就是我要找的东西

标签: javascript arrays react-native serialization


【解决方案1】:

假设这是 JavaScript,您可以使用 encodeURIComponent() 函数对您的所有键值对进行 URL 编码,如图所示。只需遍历数组并连接 URL 编码的值:

const parameters = [
  { token: '78fe6df3f'},
  { id: '12345'},
  { price: '0 - 9,000,000' },
  { 'area[]': 'Applehead Island' },
  { 'waterfront_type[]': 'Open Water' },
  { property_type_single: 'Single Family/Site Built' },
  { bedrooms: '0 - 5' },
  { baths: '0 - 5' },
  { sqft: '0 - 7500' }
];

let uri = "https://example.org/properties.php?";
let firstSegment = true;

for(const param of parameters) {
  if(!firstSegment) {
    uri += "&";
    firstSegment = false;
  }
  
  // find out the name of this object's property
  const paramName = Object.keys(param)[0];
  uri += paramName + "=" + encodeURIComponent(param[paramName]);
}

console.log(uri);

这可以用map()join()写得更简洁:

const parameters = [
  { token: '78fe6df3f'},
  { id: '12345'},
  { price: '0 - 9,000,000' },
  { 'area[]': 'Applehead Island' },
  { 'waterfront_type[]': 'Open Water' },
  { property_type_single: 'Single Family/Site Built' },
  { bedrooms: '0 - 5' },
  { baths: '0 - 5' },
  { sqft: '0 - 7500' }
];

let uri = "https://example.org/properties.php?" +
  parameters
    .map(
      param => {
        const name = Object.keys(param)[0];
        return name + "=" + encodeURIComponent(param[name]);
    })
    .join("&");
    
console.log(uri);

【讨论】:

    【解决方案2】:

    您可以将所有对象合并为一个,检查其属性并将其合并为一个字符串:

     const result = encodeURIComponent(Object.entries(Object.assign({}, ...parameters)).map(([key, value]) => key + "=" + value).join("&"));
    

    一步一步:

      [{ k: v }, { k2, v2 }]
    

    1) Object.assign({}, ...parameters)

     { k: v, k2: v2 }
    

    2) Object.entries(...)

     [[k, v], [k2, v2]]
    

    3).map(([key, value]) => key + "=" + value)

     ["k=v", "k2=v2"]
    

    4) .join("&")

     "k=v&k2=v2"
    

    【讨论】:

      【解决方案3】:

      我在堆栈溢出方面的目标是以非常简化的方式解释概念。我在代码中进行了注释,因此您将理解每一步。

      ${} 是 ES6 的概念,如果你没见过,请参考这个article

      谢谢你挑战我。

      这是我解决的代码,简短易懂。

      var parameters = [
            {token: '78fe6df3f'},
            {id: '12345'},
            { price: '0 - 9,000,000' },
            { 'area[]': 'Applehead Island' },
            { 'waterfront_type[]': 'Open Water' },
            { property_type_single: 'Single Family/Site Built' },
            { bedrooms: '0 - 5' },
            { baths: '0 - 5' },
            { sqft: '0 - 7500' }
          ];
      
      
          //we initialize an empty variable
          var serializedString = '';
      
          //.map() This loop will loop through the array to pick each object
          parameters.map((i)=>{
      
            //for-in This loop goes through the key-value inside of each object
            for(var key in i){
      
              //here we are assigning the stringed values into the variable we declared earlier on
              serializedString +=`${key}=${i[key]}&`;
            }
      
          });
      
      
          //after all this is done , we convert our string into a URL friendly string
          var ourURL_friendlyResult = encodeURIComponent(serializedString);
      
          //we console it out
          console.log(`https://example.org/properties.php?${ourURL_friendlyResult}`);
      

      【讨论】:

        猜你喜欢
        • 2018-10-05
        • 2017-04-01
        • 1970-01-01
        • 2014-03-24
        • 1970-01-01
        • 1970-01-01
        • 2021-05-29
        • 2014-08-08
        • 2018-02-13
        相关资源
        最近更新 更多