Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

convert nested json to query string

var isObj = function(a) {
  if ((!!a) && (a.constructor === Object)) {
    return true;
  }
  return false;
};
var _st = function(z, g) {
  return "" + (g != "" ? "[" : "") + z + (g != "" ? "]" : "");
};
var fromObject = function(params, skipobjects, prefix) {
  if (skipobjects === void 0) {
    skipobjects = false;
  }
  if (prefix === void 0) {
    prefix = "";
  }
  var result = "";
  if (typeof(params) != "object") {
    return prefix + "=" + encodeURIComponent(params) + "&";
  }
  for (var param in params) {
    var c = "" + prefix + _st(param, prefix);
    if (isObj(params[param]) && !skipobjects) {
      result += fromObject(params[param], false, "" + c);
    } else if (Array.isArray(params[param]) && !skipobjects) {
      params[param].forEach(function(item, ind) {
        result += fromObject(item, false, c + "[" + ind + "]");
      });
    } else {
      result += c + "=" + encodeURIComponent(params[param]) + "&";
    }
  }
  return result;
};

var data = {
  users: [{
      "name": "jeff",
      "tasks": [
        "Do one thing",
        "Do second thing"
      ]
    },
    {
      "name": "rick",
      "tasks": [
        "Never gonna give you up",
        "Never gonna let you down"
      ]
    }
  ]
}

document.write(fromObject(data));
 Run code snippet
Comment

PREVIOUS NEXT
Code Example
Javascript :: $("#right-button").click(function() { event.preventDefault(); $(".table-responsive").animate( { scrollLeft: "+=300px" }, "slow" ); }); 
Javascript :: use variable in form action vuejs 
Javascript :: angular number pipe to four decimals 
Javascript :: react-router-dom status code 301 
Javascript :: lucastools version info getter 
Javascript :: ract native hour input 
Javascript :: how to add fcc cdn to local react projcet 
Javascript :: signed url to get file from s3 bucket 
Javascript :: Add React Router to React Redux CRUD App 
Javascript :: Temporarily Edit Text on Any Website 
Javascript :: on inline style change 
Javascript :: Mutations 
Javascript :: pixijs circle 
Javascript :: Day of The Year 
Javascript :: jquery hide elevateZoom 
Javascript :: screeps clear memory of dead screeps 
Javascript :: trim angular material input with ellipsis 
Javascript :: js invert array 
Javascript :: Mandatory Parameter Shorthand javascript 
Javascript :: how to use classnames 
Javascript :: making a react js website project ready for hosting 
Javascript :: discord.js get message content 
Javascript :: make price comma jquery 
Javascript :: discord.js v12 to v13 
Javascript :: keep records of number rolled in array javascript 
Javascript :: accèder data-id javascript 
Javascript :: js wrap a function pass parameters to function 
Javascript :: crear un texto dinamicamente con javascript 
Javascript :: promtail +verbose 
Javascript :: javascript is even 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =