Home > Back-end >  Problem with convert URLSearchParams to valid object
Problem with convert URLSearchParams to valid object

Time:10-29

I need convert query string location[okrug][]=38&location[okrug][]=41 to object.

for (let [key, val] of new URLSearchParams(location.search)) {
    console.log(key) //got location except of location[okrug][]
    console.log(val) //got [object Object] except of [38, 41]
}

My bad. I passing to URLSearchParams object like this:

let filter = {
    location: {
        okrug: [38, 41]
    }
}

for (let [key, val] of new URLSearchParams(filter)) {
    console.log(key) //got location except of location[okrug][]
    console.log(val) //got [object Object] except of [38, 41]
}

In this case i need to convert object to query string and pass into constructor URLSearchParams? Then i got wrong string:

let filter = {
    location: {
        okrug: [38, 41]
    }
}

console.log(new URLSearchParams(filter).toString()) //got location=[object Object]

CodePudding user response:

My bad. I passing to URLSearchParams object like this:

let filter = {
    location: {
        okrug: [38, 41]
    }
}

for (let [key, val] of new URLSearchParams(filter)) {
    console.log(key) //got location except of location[okrug][]
    console.log(val) //got [object Object] except of [38, 41]
}

In this case i need to convert object to query string and pass into constructor URLSearchParams? Then i got wrong string:

let filter = {
    location: {
        okrug: [38, 41]
    }
}

console.log(new URLSearchParams(filter).toString()) //got location=[object Object]

https://skr.sh/sGd4pg5KyaY

CodePudding user response:

I am stupid :) I am again was wrong. I just need to right inicialize filter:

let filter = this.pageFilter || new URLSearchParams(location.search)

Thanks to all :D

  • Related