Home  >  Article  >  Web Front-end  >  Instructions for using the querystring.stringify method in node.js_node.js

Instructions for using the querystring.stringify method in node.js_node.js

WBOY
WBOYOriginal
2016-05-16 16:27:471763browse

Method description:

Convert the object into a string. Multiple parameters in the string will be separated by ‘&’ and assigned with ‘=’.

The operation of this function is opposite to querystring.parse() . You can understand it by looking at the example.

Grammar:

Copy code The code is as follows:

querystring.stringify(obj, [sep], [eq])

Receive parameters:

obj The object to be converted
sep Set the separator, the default is '&'
eq                                                                                                                                               out out out of 5

Example:

Copy code The code is as follows:
querystring.stringify({ foo: 'bar', baz: ['qux', 'quux'], corge: '' })
// returns
'foo=bar&baz=qux&baz=quux&corge='

querystring.stringify({foo: 'bar', baz: 'qux'}, ';', ':')
// returns
'foo:bar;baz:qux'

Source code:

Copy code The code is as follows:
QueryString.stringify = QueryString.encode = function(obj, sep, eq, name) {
sep = sep || '&';
eq = eq || '=';
if (util.isNull(obj)) {
Obj = undefined;
}
if (util.isObject(obj)) {
Return Object.keys(obj).map(function(k) {
var ks = QueryString.escape(stringifyPrimitive(k)) eq;
If (util.isArray(obj[k])) {
          return obj[k].map(function(v) {
              return ks QueryString.escape(stringifyPrimitive(v));
           }).join(sep);
} else {
            return ks QueryString.escape(stringifyPrimitive(obj[k]));
}
}).join(sep);
}
if (!name) return '';
return QueryString.escape(stringifyPrimitive(name)) eq
QueryString.escape(stringifyPrimitive(obj));
};

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn