From ac076eeaaede78a136b664f201f95fc9fdce7695 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Wed, 16 Oct 2019 09:35:02 +0300 Subject: [PATCH] Update objectToQueryString.md --- snippets/objectToQueryString.md | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/snippets/objectToQueryString.md b/snippets/objectToQueryString.md index ed3bfaf7d..7d6f2f67c 100644 --- a/snippets/objectToQueryString.md +++ b/snippets/objectToQueryString.md @@ -3,12 +3,11 @@ title: objectToQueryString tags: utility,object,function,intermediate --- -Generates a query string from key values pairs from given object. +Returns a query string generated from the key-value pairs of the given object. -Attaches `?` in front of query string and ignores any non-string -values and their corresponding keys from given object. The function -returns empty string as a result if there is no single valid -key-value pair in given object, else returns generated query string. +Use `Array.prototype.reduce()` on `Object.entries(queryParameters)` to create the query string. +Determine the `symbol` to be either `?` or `&` based on the `index` and concatenate `val` to `queryString` only if it's a string. +Return the `queryString` or an empty string when the `queryParameters` are falsy. ```js const objectToQueryString = (queryParameters) => {