0

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+$/));
}

array result

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!

2
  • Sorry didnt understand but what are you trying to do? Assign new values to the keys? Commented Aug 26, 2015 at 16:36
  • 1
    Use an object, not dynamically named variables. The latter is prone to all sorts of unexpected failures and security vulnerabilities. Commented Aug 26, 2015 at 16:38

4 Answers 4

2

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>

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks very much for your code, in any case, if I wanted to assign the values to static variables, how can I achieve this? please see my updated question.
@J.D: "...if I wanted to assign the values to static variables..." You don't. :-) I know you think you do, but that road is just (to use an 80s word) gnarly. The code to do it is convoluted, error-prone, has poor performance, is difficult to maintain, and most importantly of all is unnecessary. Using an object (or an array) is the right way to handle this kind of dynamic situation. The update to your question shows you assigning to a testUser variable, but as you flagged up, there would need to be a dynamic number of them. That's what objects and arrays are for.
1

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"]

Comments

1

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

Comments

1

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ł

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.