简体   繁体   中英

Create variables based on array

I have the following array and a loop fetching the keys ( https://jsfiddle.net/ytm04L53/ )

var i;
var feeds = ["test_user_201508_20150826080829.txt:12345","test_user_list20150826:666","test_list_Summary20150826.txt:321"];

for (i = 0; i < feeds.length; i++) {
    var feed = feeds[i];
    alert(feed.match(/\d+$/));
}

数组结果

The array will always contain different number of keys, What I would like to do is either use these keys as variables and assign the value after the : semicolon as its value or just create a new set of variables and assign the values found on these keys to them.

How can I achieve this? so that I can then perform some sort of comparison

if (test_user > 5000) {dosomething}

update Thanks for the answers, how can I also create a set of variables and assign the array values to them? For instance something like the following.

valCount(feeds.split(","));

    function valCount(t) {
     if(t[0].match(/test_user_.*/))
      var testUser = t[0].match(/\d+$/);
     }

Obviously there is the possibility that sometimes there will only be 1 key in the array and some times 2 or 3, so t[0] won't always be test_user_

I need to somehow pass the array to a function and perform some sort of matching, if array key starts with test_user_ then grab the value and assign it to a define variable.

Thanks guys for all your help!

You can't (reasonably) create variables with dynamic names at runtime. (It is technically possible.)

Instead, you can create object properties:

var feeds = ["test_user_201508_20150826080829.txt:12345","test_user_list20150826:666","test_list_Summary20150826.txt:321"];

var obj = {};
feeds.forEach(function(entry) {
    var parts = entry.split(":"); // Splits the string on the :
    obj[parts[0]] = parts[1];     // Creates the property
});

Now, obj["test_user_201508_20150826080829.txt"] has the value "12345" .

Live Example:

 var feeds = ["test_user_201508_20150826080829.txt:12345","test_user_list20150826:666","test_list_Summary20150826.txt:321"]; var obj = {}; feeds.forEach(function(entry) { var parts = entry.split(":"); obj[parts[0]] = parts[1]; }); snippet.log(obj["test_user_201508_20150826080829.txt"]); 
 <!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --> <script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script> 

You can do it like this, using the split function:

var i;
var feeds = ["test_user_201508_20150826080829.txt:12345","test_user_list20150826:666","test_list_Summary20150826.txt:321"];

for (i = 0; i < feeds.length; i++) {
    var feed = feeds[i];
    console.log(feed.split(/[:]/));
}

This outputs:

["test_user_201508_20150826080829.txt", "12345"]
["test_user_list20150826", "666"]
["test_list_Summary20150826.txt", "321"]

Use the split method

var feeds = ["test_user_201508_20150826080829.txt:12345","test_user_list20150826:666","test_list_Summary20150826.txt:321"];

feedMap = {}
for (i = 0; i < feeds.length; i++) {
    var temp = feeds[i].split(':');
    feedMap[temp[0]] = temp[1];
}

Yields:

{
    "test_user_201508_20150826080829.txt":"12345",
    "test_user_list20150826":"666",
    "test_list_Summary20150826.txt":"321"
}

And can be accessed like:

feedMap["test_user_201508_20150826080829.txt"]

Here is a codepen

it is not very good idea but if you really need to create variables on-the-run here's the code:

for (i = 0; i < feeds.length; i++) 
{
    var feed = feeds[i];
    window[feed.substring(0, feed.indexOf(":"))] = feed.match(/\d+$/);
}

alert(test_user_201508_20150826080829)

Of course you cannot have any variable-name-string containing banned signs (like '.')

Regards, Michał

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM