how to pass an array of objects through a query string according to rest? in typescript

index.ts
// Assuming you have an array of objects
const data = [
  { name: 'Alice', age: 30 },
  { name: 'Bob', age: 35 }
];

// Convert the array of objects to a JSON string
const json = JSON.stringify(data);

// Encode the JSON string to make it safe to be passed in a query string
const encodedData = encodeURIComponent(json);

// Now you can pass this encoded data as part of your query string
const url = `https://example.com/api?data=${encodedData}`;

// On the receiving end, you would decode the data from the query string
const queryString = location.search;
const params = new URLSearchParams(queryString);
const receivedData = params.get('data');
const decodedData = JSON.parse(decodeURIComponent(receivedData));
715 chars
21 lines

related categories

gistlibby LogSnag