Setting an object to a split array

I am not a javascript guru. I have the following code below:

var aCookieValues = sCookieContentString.split('&'); // split out each set of key/value pairs
var aCookieNameValuePairs = aCookieValues.split('='); // return an array of each key/value

What I'm trying to do is split the first line through and then create another array that takes the first array and breaks it further with the = character that exists in every value in the aCookieValues ​​array

I get an error. aCookieValues.split is not a function.

I saw an example that basically does the same thing, but the second time this guy uses a loop:

( http://seattlesoftware.wordpress.com/2008/01/16/javascript-query-string/ )

    // '&' seperates key/value pairs
    var pairs = querystring.split("&");

    // Load the key/values of the return collection  
    for (var i = 0; i < pairs.length; i++) {
        var keyValuePair = pairs[i].split("=");
        queryStringDictionary[keyValuePair[0]] = keyValuePair[1];
    }

, , - /, "=". cookie , .

+3
3

, aCookieValues - , split. split aCookieValues:

var aCookieValues = sCookieContentString.split('&');

for (var i = 0; i < aCookieValues.length; i++) {
   var aCookieNameValuePairs = aCookieValues[i].split('=');

   // Handle aCookieNameValuePairs[0] as the key
   // Handle aCookieNameValuePairs[1] as the value
}

, for: var myDict = {}, split('=') :

myDict[aCookieNameValuePairs[0]] = aCookieNameValuePairs[1];

:, , , , . , , , :)

+3

split . aCookieValues, . , , - , .

map, . , :

if (!Array.prototype.map) { // don't step on anyone toes
  Array.prototype.map = function( f ) {
    var result = [];
    var aLen = this.length;
    for( x = 0 ; x < aLen ; x++ ) {
      result.push( f(this[x]) );
    }
    return result; 
  };
};

... . :

​yourstring = 'x=3&y=4&zed=blah&something=nothing';
dictionary = yourstring.split('&').map( function(a){ return a.split('='); } );

dictionary () /, :

[["x", "3"], ["y", "4"], ["zed", "blahblah"], ["something", "nothing"]]

, . , , , , , .

+1

In the second line, you are trying to call split () in the array when it is a function defined in the lines.

Example:

"a=1&b=2&c=3".split('&') returns an array ['a=1','b=2','c=3']

Then your code will call split in the array:

['a=1','b=2','c=3'].split('=')

But this function does not exist. It seems your goal is to split each row in the array, so the example you asked in the question seems appropriate - swipe through each element and separate it.

+1
source

Source: https://habr.com/ru/post/1754724/


All Articles