2

What I mean is, say I have this object:

var myObject = [{
  name:"Name1",
  text:"text1",
  array:"firstArray"
},{
  name:"Name2",
  text:"text2",
  array:"secondArray"
}];

Later in the code there is this:

useArray=[];
firstArray=['a','b','c'];
secondArray=['x','y','z'];

Then, there's a conditional statement that decides which array will be used, but instead of hand typing it out like this:

if(selection = "Name1"){
  useArray = firstArray;
}
else if(selection = "Name2"){
  useArray = secondArray;
}

I want to be able to have it dynamic, so that no matter how long the object is it will work. Like this:

for(i=0;i<myObject.length;i++){
   if(selection == myObject[i].name){
       useArray = myObject[i].array;
   }
}

But unfortunately this doesn't work. In the object I've even tried taking out the quotation marks around the array and declaring the array before the object but that doesn't work either.

The array comes up empty or undefined.

I know it has to do with being a string or something so what is the workaround to accomplishing this?


I realize I should have made my arrays differently. What if the arrays are actually just more objects? The answers below look beautiful and I think Amit's and other answers that look similar will do the trick. But the difference with the problem I am actually attempting to solve is that first of all, my arrays start out empty, so here I have:

var myObject = [{
  name:"Name1",
  text:"text1",
  array:[]
},{
  name:"Name2",
  text:"text2",
  array:[]
}];

Then I've added:

var arrays={
    firstArray:[],
    secondArray:[]
}

Again, it starts out empty. As specified in my original question, later in the code the arrays are filled. To be more exact and clear, this is how it is being done:

arrays.firstArray.push({
  floor:myObjects[c].name, text:myObject[c].text
});

It is in a for loop but that is irrelevant.

My for loop set up just like this one:

for(i=0;i<myObject.length;i++){
  if(selection == myObject[i].name){
    useArray = arrays[myObject[i].array];
  }
}

But when I alert useArray, it is blank. Is this because the array I am using is actually an object?

Christine268
  • 722
  • 2
  • 13
  • 32
  • maybe this helps http://stackoverflow.com/questions/684672/loop-through-javascript-object – user2879041 Jul 30 '15 at 18:29
  • Would it be possible to just have your `firstArray` be a member of the `myObject[1]` object, and `secondArray` be a member of the `myObject[2]` object? Edit: Looks like someone beat me to it in an answer. Would that work for your use case though? – Maximillian Laumeister Jul 30 '15 at 18:30
  • @user2879041 I am not actually sure what you mean. – Christine268 Jul 30 '15 at 20:14

8 Answers8

2

Variable names have no meaning and can't be accessed by name in JavaScript. It is for this reason that minification processes will replace (and shorten) the names of variables.

You could have meaningful names as properties of an object.

To apply that to your question, you can do one of:

  1. Have your original object contain the array directly:

    var myObject = [{
        name:"Name1",
        text:"text1",
        array:['a','b','c']
      },{
        name:"Name2",
        text:"text2",
        array:['x','y','z']
    }];
    
  2. Have a separate object contain the arrays as named properties:

    var arrays = {
      firstArray: ['a','b','c'],
      secondArray: ['x','y','z']
    }
    var useArray;
    for(i=0;i<myObject.length;i++){
      if(selection == myObject[i].name){
        useArray = arrays[myObject[i].array];
      }
    }
    
Amit
  • 45,440
  • 9
  • 78
  • 110
  • Please see my edit. I should have been more specific that I am using an object vs a simple array. I would have thought it would come out the same in the wash but I don't know if it isn't working quite right because mine is starting out empty in `myObject` and being filled later. – Christine268 Jul 30 '15 at 20:26
  • I read you're edit, but I don't understand what your code is doing, or why you have the arrays in 2 places to begin with. Can you demonstrate your use case with a working sample (read [ask], create MCVE) – Amit Jul 30 '15 at 20:31
  • I don't have arrays in 2 places. I have an object that states which array each value will use, then I have the object of arrays you told me to create. Then, where the .push() is at, that is, lets say a function, where something happens that adds values to the array. – Christine268 Jul 30 '15 at 20:53
  • Maybe you misunderstood my answer then... if you go with the 2nd option, you need to leave your `myObject` exactly as it was, particularly, `array:"firstArray"`. – Amit Jul 30 '15 at 20:56
  • I did misunderstand the question then. – Christine268 Jul 30 '15 at 21:04
  • Yup, literally all I had to do was change the arrays back to array:"firstArray" and it worked. Thank you thank you. Sorry, I misread your answer at first, totally missed the 1 & 2. I appreciate your guidance. – Christine268 Jul 30 '15 at 21:17
1

Working JSFiddle. Try using forEach:

myObject.forEach(function(item) {
  if (selection === item.name) {
    useArray = item.array
  }
}

Your arrays can absolutely be referenced by name in myObject. Take off the quotes from your variable name and declare your arrays above myObject. Full code:

var useArray=[];
var firstArray=['a','b','c'];
var secondArray=['x','y','z'];
var selection = "Name2";

var myObject = [{
      name:"Name1",
      text:"text1",
      array:firstArray
    },{
      name:"Name2",
      text:"text2",
      array:secondArray
    }];

myObject.forEach(function(item) {
  if (selection === item.name) {
    useArray = item.array
  }
})

console.log(useArray);
Pavan Ravipati
  • 1,890
  • 14
  • 21
1

You could have an array list and set your useArray inside your for() by getting its name. Just like this:

var arrayList = {
  firstArray: ['a','b','c'],
  secondArray:['x','y','z']
};

then:

for(i=0;i<myObject.length;i++){
   if(selection == myObject[i].name){
       useArray = arrayList[myObject[i].array];
   }
}

For clarifying why that works:

Given

var foo = {bar: 1};

You could access your bar property with any of these syntax:

foo.bar // returns 1
foo['bar'] // returns 1
Felipe Skinner
  • 16,246
  • 2
  • 25
  • 30
0

Change it to:

var myObject = [{
  name:"Name1",
  text:"text1",
  array:['a','b','c']
},{
  name:"Name2",
  text:"text2",
  array:['x','y','z']
}];

You are assigning to a string, but just having a string with the name binding doesn't work. You need to assign to the actual array.

iobender
  • 3,346
  • 1
  • 17
  • 21
0

I'd suggest you store a reference to the actual array inside your object instead, like so.

var firstArray=['a','b','c'];
var secondArray=['x','y','z'];

var myObject = [{
  name:"Name1",
  text:"text1",
  array:firstArray
},{
  name:"Name2",
  text:"text2",
  array:secondArray
}];

var selection = "Name1";

for(i=0;i<myObject.length;i++){
   if(selection == myObject[i].name){
       alert(myObject[i].array);
   }
}
Jan
  • 5,688
  • 3
  • 27
  • 44
0

If what you're trying to do is reference an array by a string, you might be able to do it this way:

var selection = 'Name2';

var myObject = [{
  name:"Name1",
  text:"text1",
  array:"firstArray"
},{
  name:"Name2",
  text:"text2",
  array:"secondArray"
}];

useArray=[];
allArrays = {
  firstArray:['a','b','c'],
  secondArray:['x','y','z']
}

for(i=0;i<myObject.length;i++){
  if(selection == myObject[i].name){
    useArray = allArrays[myObject[i].array];
  }
}

console.log(useArray);
Joseph Marikle
  • 76,418
  • 17
  • 112
  • 129
0

Try with:

for(i=0;i<myObject.length;i++){
   if(selection == myObject[i].name){
       useArray = window[myObject[i].array];
       //         ^^^^^^
   }
}    
MazzCris
  • 1,812
  • 1
  • 14
  • 20
0

Try this:

for(key in myObject){
    if(section == myObject[key].name){
        useArray = myObject[key].array;
    }
}

Here is the Fiddle: http://jsfiddle.net/ydxf4b0g/

Sergey6116
  • 124
  • 5