JavaScript: Parsing URL Parameters from String to Object Format

Converts the format “a=1&b=2” to {a: “1”, b: “2”}

module.exports = {
str2Obj: function (str) {
if ("string" != typeof str) return str
if (str.indexOf("&") < 0 && str.indexOf("=") < 0) return {}
let newStr = str.split("&"),newOjb = {}
newStr.forEach(value => {
if (value.indexOf("=") > -1) {
let newStr1 = value.split("=")
newOjb[newStr1[0]] = newStr1[1]
}
})
return newOjb
}
}

// test
console.log(str2Obj("a=1&b=2"))
//log:{a: "1", b: "2"}