0

I have an array of items, and I am trying to assign a variable name based on each of the array's item names. For example, if the array has the values:

var thearray = {};
thearray['0'] = 'car';
thearray['1'] = 'boat';
thearray['2'] = 'truck';

I want to end up with three variables, named car_output, boat_output, truck_output.

They will all contain the same data. Here is how I'm trying to do it:

$.each(thearray, function(index, value) { 
    var value+'_output' = 'stuff...'; // something not right
});

Something is not right with the way I'm trying to name each variable. Any ideas?

3
  • 'var value+'_output' = 'stuff...';` ??? why is there + sign and a constant '_output'? The LHS should be a variable only Commented Aug 6, 2014 at 4:43
  • @JROB - Look at bala answer it is perfect answer for your question Commented Aug 6, 2014 at 5:12
  • @JqueryKing Thanks... once I get the arrays loaded, I can't get them onto the page: $('#'+value).html(value+'_output');. It's just showing the actual word, for example "car_output" and not what the var "car_output" actually is. Commented Aug 6, 2014 at 5:15

4 Answers 4

1

This should do the trick. The secret is to use the global window object like an array, in order to create dynamic variable names.

var thearray = {};
thearray['0'] = 'car';
thearray['1'] = 'boat';
thearray['2'] = 'truck';

$.each(thearray, function(index, value) { 
    window[value+'_output'] = 'stuff...';
});

console.log(car_output); // "stuff..."
Sign up to request clarification or add additional context in comments.

1 Comment

One problem I'm having is trying to output the variable contents into a div: $('#'+value).html(value+'_output');... it's outputting the text, for example car_output, and not what the variable car_output actually holds.
0

Try to create an object dynamically to store the relevant values, please don't use window object to store the values since it is a clumsy way.

var thearray = {};
thearray['0'] = 'car';
thearray['1'] = 'boat';
thearray['2'] = 'truck';

var obj={};
$.each(thearray, function (index, value) {
    obj[value+"_output"]="stuff";
});

DEMO

1 Comment

@Downvoter: Care to comment.? what's wrong in my answer.
0

you can do using array,

var user_types = ["car","boat","truck"];
var types = {};

$.each(user_types, function( index, value ){
  types[value+"_output"] = 'The value of dynamic variable, val';
});

console.log(types);

i hope this help you..

Comments

0

I think this is what you wanted. it's working as you wanted.

var user_types = ["car","boat","truck"];
var types = {};

$.each(user_types, function( index, value ){
  types[value+'_Output'] = value;
});

console.log(types);

see jsfiddle link http://jsfiddle.net/C23U8/1/

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.