aleXela
aleXela

Reputation: 1301

slice string to pieces and store each in new array

Help needed. I have string like ["wt=WLw","V5=9jCs","7W=71X","rZ=HRP9"] (unlimited number of pairs)

I need to make an array with pair like wT (as index) and WLw as value, for the whole string (or simpler wT as index0, WLw as index 1 and so on) I'm trying to do it in JavaScript but I just cant figure out how to accomplish this task.

Much much appreciate your help!!

Upvotes: 0

Views: 197

Answers (2)

NickSlash
NickSlash

Reputation: 5077

You cannot have a string as an index in an array, what you want is an object.

All you need to do is loop over your array, split each value into 2 items (key and value) then add them to an object.

Example:

// output is an object
var output = {};
var source = ["wt=WLw","V5=9jCs","7W=71X","rZ=HRP9"];
for (var index = 0; index < source.length; index++) {
    var kvpair = source[index].split("=");
    output[kvpair[0]] = kvpair[1];
}

If you wanted an array of arrays, then its much the same process, just pushing each pair to the output object

// output is a multidimensional array
var output = [];
var source = ["wt=WLw","V5=9jCs","7W=71X","rZ=HRP9"];
for (var index = 0; index < source.length; index++) {
    output.push(source[index].split("="));
}

Update If your source is actually a string and not an array then you will have to do a little more splitting to get it to work

var output = {};
var sourceText = "[\"wt=WLw\",\"V5=9jCs\",\"7W=71X\",\"rZ=HRP9\"]";
// i have escaped the quotes in the above line purely to make my example work!
var source = sourceText.replace(/[\[\]]/g,"").split(",");
for (var index = 0; index < source.length; index++) {
    var kvpair = source[index].split("=");
    output[kvpair[0]] = kvpair[1];
}

Update 2

If your desired output is an array of arrays instead of an object containing key-value pairs then you will need to do something like @limelights answer.

Object with Key-Value pairs: var myObject = { "key1": "value1", "key2": "value2" };

with the above code you can access "value1" like this myObject["key1"] or myObject.key1

Array of Arrays: var myArray = [ [ "key1", "value1"] ,[ "key2", "value2" ] ];

with this code, you cannot access the data by "key" (without looping through the whole lot to find it first). in this form both "key1" and "value1" are actually values.

to get "value1" you would do myArray[0][1] or you could use an intermediary array to access the pair:

var pair = myArray[0];
> pair == ["key1", "value1"]
> pair[0] == "key1"
> pair[1] == "value1"

You can use a for each loop on both types of result

// array of arrays
var data = [ [ "hello", "world"], ["goodbye", "world"]];

data.forEach(function(item) {
    console.log(item[0]+" "+item[1]);
});
> Hello World
> Goodbye World

// object (this one might not work very well though)
var data = { "hello": "world", "goodbye": "world" };

Object.keys(data).forEach(function(key) {
    console.log(key+" "+data[key]);
});
> Hello World
> Goodbye World

Upvotes: 3

Henrik Andersson
Henrik Andersson

Reputation: 47172

The normal for loop would do perfectly here!

var list = ["wt=WLw","V5=9jCs","7W=71X","rZ=HRP9"];
var pairs = [];
for(var i = 0; i < list.length; i++){
   pairs.push(list[i].split('='));
}

This would give you an array of pairs, which I assume you want. Otherwise just get rid of the outer Array and do list[i].split('=');

If you want it put into an object ie. not an Array

var pairObject = {};
for(var i = 0; i < list.length; i++){
    var pair = list[i].split('=');   
    pairObject[pair[0]] = pair[1];
}

Upvotes: 2

Related Questions