S
S
speculant2018-08-06 00:19:04
JavaScript
speculant, 2018-08-06 00:19:04

How to convert three arrays to url and back?

Hello, there are three arrays, they store information in this form

el_1: "option_1_3", el_2: "option_1_2" //первый массив
el_1: "option_2_2", el_2: "option_2_3" //второй
el_1: "option_3_1", el_2: "option_3_2" // третий

I need to make a link so that the values ​​\u200b\u200bof these arrays are transmitted in it, and it is possible to parse the link to display the same elements. I found the http_build_query.js
function with its help I get a line like
var query = build_query(arr1, 5) +'%' + build_query(arr2, 5) +'%' + build_query(arr3, 5);
//результат
query = el_1=option_1_3&el_2=option_1_2%el_1=option_2_2&el_2=option_2_3%el_1=option_3_1&el_2=option_3_2

How to split it back into three arrays? Or maybe some other way to solve

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ihor Bratukh, 2018-08-06
@speculant

There is jQuery in the question tags, if you use it, then why do you need http_build_query.js$ .param() to translate back, you can create your own small method or googleunparam, deparam

;(function($) {
    $.extend({
        unparam: function(param) {
            if (param[0] === '?') param = param.slice(1);
            var re = {};
            for (var i = 0, arr = param.split('&'), kv; kv = arr[i]; i++) {
                kv = kv.split('=');
                re[kv[0]] = kv[1];
            }
            return re;
        }
    });
})(jQuery);

Example:
var param = $.param({
  el_1: 'option_1_3',
  el_2: 'option_1_2'
});
//> "el_1=option_1_3&el_2=option_1_2"

var decode = $.unparam(param);
//> {el_1: "option_1_3", el_2: "option_1_2"}

L
Leo Mavrin, 2018-08-06
@Leo5878

In your question, I would recommend using objects.
If I understood correctly, then you need to get the values ​​of each key!?
"and it was possible to parse the link to display the same elements" - Do you mean parse the link?
As an object, it would look like this:

{
el_1 : "option_1_3", 
el_2 : "option_1_2",
el_3 : "option_2_2", 
el_4 : "option_2_3", 
el_5 : "option_3_1",
el_6 : "option_3_2"
}

Read this , I think you will find something for yourself that will help you in solving the problem

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question