0

I have an object similar to below

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'
 };

I want this object to be turned to like below https://www.example.com/properties.php?token=78fe6df3f&id=12345&price=0ドル%20-%203,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 How can I get this in react native. In javascript $.param(obj) does this job. Please guide me.

I want the above to do fetch calls in react native. The object will be generated by the filter form.

asked Oct 11, 2018 at 11:11
2
  • I am sure if you google for js url builder library you can find a lot of 3rd party dependencies which process object into query params. Commented Oct 11, 2018 at 11:15
  • This looks promising. You can pass the object as you described into this and it will be appended as url query params Commented Oct 11, 2018 at 11:16

1 Answer 1

2

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));

answered Oct 11, 2018 at 11:22
Sign up to request clarification or add additional context in comments.

1 Comment

This is nice but an explanation should be nice.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.