0

I created a function which allows to return an object whose name change to each loop.

I done this function like this :

function createObjPack(index){
    var currentPack = packVehicule[key].libelle;

    return [eval(currentPack + ' = {}' ), calcul(currentPack, key)];
};

The variable curentPack contains the name of the current object. The return must generate a object which name match the value of currentPack

I thought read the currentPack directly into an eval() function for change dynamicly the name but, it doesn't work.

Someone can help me ?

2
  • I think the parameter index should be named key shouldn't it? Commented Jul 30, 2014 at 9:20
  • Dnamic variable names seems like the wrong thing. Use an object whose keys are the dynamic names. And you don't need to use eval to create dynamic variable names, just assign window[currentPack], since all global variables are properties of window. Commented Jul 30, 2014 at 9:22

1 Answer 1

1

Don't use dynamic variable names, use an object.

var packs = {}

function createObjPack(index) {
    var currentPack = packvehicule[index].libelle;
    var newPack = {};
    packs[currentPack] = newPack;
    return [newPack, calcul(currentPack, index)];
}
Sign up to request clarification or add additional context in comments.

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.