-1

I needed to convert an object to query parameters

I have used the code suggested here for the recursive function

const serializeObject = (obj, label)  => {
    const pairs = [];
    for (const prop in obj) {
        if (!obj.hasOwnProperty(prop)) {
            continue;
        }

        if (typeof obj[prop] === 'object') {
            pairs.push(serializeObject(obj[prop]), prop);
            continue;
        }


        if (label !== undefined) {
            pairs.push(
                "pageSearch[" + label + "][" + prop + "]" + "=" + obj[prop]
            );
        } else {
            pairs.push("pageSearch[" + prop + "]" + "=" + obj[prop]);
        }

    }
    return pairs.join("&");
};

I have added the 2nd parameter to the functions because I needed parameters in this format pageSearch[li_date][datefrom] and pageSearch[li_date][dateto]

But the 2nd parameter label always remains undefined. Please suggest what's wrong here?

JavaScript Object for testing the function:

{
"pageSearch": {
    "id": "",
    "agent_name": "",
    "access": "",
    "li_date": {
        "datefrom": "04/28/2022 02:15 PM",
        "dateto": "04/28/2022 02:15 PM"
    },
    "email": "",
    "phone": "",
    "date": {
        "datefrom": "04/28/2022 02:15 PM",
        "dateto": "04/28/2022 02:15 PM"
    },

}

}

Alok Jain
  • 3,379
  • 3
  • 23
  • 42
  • Are you only going to need datefrom and dateto from li_date or need any other values from pagSearch to be added in queryparams? – Inder Apr 28 '22 at 09:15
  • I need all values from that object in query params – Alok Jain Apr 28 '22 at 09:33
  • Take a look at this [codesandbox](https://codesandbox.io/s/flamboyant-rain-fhlhhp?file=/src/index.js). This will return all the params in array of objects and then you can loop over that array and add it inside query parameters. Check the console log – Inder Apr 28 '22 at 09:43
  • Thanks, @Inder but I am getting keys for that object dynamically through an API, so I can not hardcode them. I need a function that returns both parameters (from the key) and value. – Alok Jain Apr 28 '22 at 09:59
  • Yes, you can simply use this function and pass in parameters. The loop will work and return data in array – Inder Apr 28 '22 at 10:00
  • simply add a parameter data in function parameters and pass in the object. It will work. – Inder Apr 28 '22 at 10:01
  • parameters are key names used in the object – Alok Jain Apr 28 '22 at 10:08
  • Is the basic data structure always going to be the same like you have here. ? Like this nested object? – Inder Apr 28 '22 at 10:10
  • Thanks, @Inder your code worked for me with little modifications. However, it's not recursive. I would try to make it recursive so that it works with any level of nesting, and also make this more re-usable. – Alok Jain Apr 28 '22 at 10:22

1 Answers1

0

Why don't you just do

url = "http//someurl/?obj=" + encodeURIComponent(JSON.stringify(myObject));

and on the other side you decode it like this:

const myObj = JSON.parse(new URLSearchParams(window.location.search).get("obj"))
lviggiani
  • 5,824
  • 12
  • 56
  • 89
  • I need to send this data to an API, unfortunately I do not have any control over other side (the receiver side) – Alok Jain Apr 28 '22 at 09:31