代码之家  ›  专栏  ›  技术社区  ›  m9m9m

如何在react native中将对象转换为序列化对象?

  •  1
  • m9m9m  · 技术社区  · 6 年前

    我有一个类似于下面的物体

    const params = {
          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中 $.param(obj) 做这个工作。请引导我。

    我想做上述的反应本机获取调用。对象将由筛选器窗体生成。

    2 回复  |  直到 6 年前
        1
  •  2
  •   Dan    6 年前

    const paramsToString = params => Object.entries(params).reduce((acc, [key, value], index, array) => `${acc}${key}=${encodeURIComponent(value)}${index !== (array.length - 1) ? '&' : ''}`, "");
    
    const params = {
          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'
        };
        
    console.log(paramsToString(params));