-2

I have these objects that are stored in array :

var current = [{1:{X:"",Y:""},2:{X:"",Y:""},3:{X:"",Y:""}},
           {1:{X:"",Y:""},2:{X:"",Y:""},3:{X:"",Y:""}},
           {1:{X:"",Y:""},2:{X:"",Y:""},3:{X:"",Y:""}}
           ];

What I want is to store them dynamically and access them dynamically after passing the objects value in a function.

function getXAxis(num){
//any
}

function getYAxis(num){
//any
}

trial1a.X = getXAxis(current[0][1].X);
trial1a.Y = getYAxis(current[0][1].Y);
trial2a.X = getXAxis(current[0][2].X);
trial2a.Y = getYAxis(current[0][2].Y);
trial3a.X = getXAxis(current[0][3].X);
trial3a.Y = getYAxis(current[0][3].Y);
trial1b.X = getXAxis(current[1][1].X);
trial1b.Y = getYAxis(current[1][1].Y);

What is the best way to access current values and store them dynamically as well

ajbee
  • 3,511
  • 3
  • 29
  • 56
  • It's not clear to me what the issue is. You are already accessing the values in `current`. – Felix Kling Oct 29 '15 at 03:07
  • Please note in your definition you're using capital `"X"`, `"Y"` but when trying to lookup you're using lower `"x"`, `"y"` – Paul S. Oct 29 '15 at 03:07
  • @FelixKling I'm accessing them but not dynamically – ajbee Oct 29 '15 at 03:08
  • 1
    Then use a variable instead of any of the numbers. E.g. `current[i]`, where `i` gets its value from wherever. Is that what you are asking for? If you have ever iterated over an array with a `for` loop, you know how to access an array "dynamically". – Felix Kling Oct 29 '15 at 03:09
  • Regarding adding: [Add new object to array](http://stackoverflow.com/q/9543805/218196). – Felix Kling Oct 29 '15 at 03:10
  • "dynamically" in the sense that I don't need to declare trial1a.x... one by one – ajbee Oct 29 '15 at 03:13
  • So you actually want to *create variables* dynamically? That's almost always a bad idea. You should have an array of `trial` elements instead (but then again you already have an array of objects). Not really sure where you are going with all of this.... maybe you are rather looking for `.map`? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map – Felix Kling Oct 29 '15 at 03:16

2 Answers2

1

I think what you want is to have your trials be dynamically generated?

Something like this should do what you want:

var trials = [];
for (var i = 0; i < current.length; i++) {
    trials[i] = {};
    for (var j in current[i]) {
        trials[i][j] = {
            X: getXAxis(current[i][j].X),
            Y: getYAxis(current[i][j].Y)
        };
    }
}
Steven Moseley
  • 15,871
  • 4
  • 39
  • 50
0

I guess I know what you are looking for..

var root = {};
function setValueInDeepStructure(value, path, tree) {  
  var thisStep = path.shift();
  if (path.length == 0) {
    tree[thisStep] = value;
  } else {
    if (!tree[thisStep]) {
      tree[thisStep] = {};
    }

    setValueInDeepStructure(value, path, tree[thisStep]);
  }
}

function getValueFromDeepStructure(path, tree) {
  var thisStep = path.shift();
  if (path.length == 0)
    return tree[thisStep];
  else
    return getValueFromDeepStructure(path, tree[thisStep]);
}

setValueInDeepStructure({x: 3, y: 5}, [0,1,5,6], root);
setValueInDeepStructure(7, [0,1,5,6,'x'], root);

getValueFromDeepStructure([0,1,5,6], root) // => {x:7, y:5}
webdeb
  • 12,993
  • 5
  • 28
  • 44