0

I have 2 global variables that I would like to use in place of YYYYYY and XXXXXX but cant seem to figure out how to make it happen. Can anyone offer a suggestion? Thanks

var channelKeys = [];
channelKeys.push({
  channelNumber: YYYYYY,
  name: 'Location1',
  key: 'XXXXXX',
  fieldList: [{
    field: 1,
    axis: 'T'
  }, {
    field: 2,
    axis: 'T'
  }, {
    field: 3,
    axis: 'T'
  }]
});
FrankL
  • 13
  • 4

3 Answers3

0
Global Variables:
var var1='test'
var var2='test1'

We Can assign the value after intialization of channelKeys like:

channelKeys[0].channelNumber=x;
channelKeys[0].key=x;

or can be intialized with channelKeys:

var channelKeys = [];
channelKeys.push({
  channelNumber: var1,
  name: 'Location1',
  key: var2,
  fieldList: [{
    field: 1,
    axis: 'T'
  }, {
    field: 2,
    axis: 'T'
  }, {
    field: 3,
    axis: 'T'
  }]
});
mohit uprim
  • 5,226
  • 2
  • 24
  • 28
0

You can go with below kind of stuff. Are you expecting something else ?

var firstVariable = 'YYYYY';
var secondVariable = 'XXXXX';
var channelKeys = [];
channelKeys.push({
  channelNumber: firstVariable,
  name: 'Location1',
  key: secondVariable,
  fieldList: [{
    field: 1,
    axis: 'T'
  }, {
    field: 2,
    axis: 'T'
  }, {
    field: 3,
    axis: 'T'
  }]
});

If you are looking to loop in to the array and replace you can use like the below. If both the first and second variables should be array with same length, You can use the index as [index] near the first and Second variable name.

$.each(channelKeys, function(index){
channelKeys[index].channelNumber = firstVariable;
channelKeys[index].key= secondVariable;

});
  • Those variables dont make it into the array. they come out as the string firstVariable secondVariable – FrankL Nov 26 '17 at 06:48
0

Global variables can be assigned to channelKeys like:

var XXXXXX=12;
var YYYYYY =34;

var channelKeys = [];
channelKeys.push({
  channelNumber: YYYYYY,
  name: 'Location1',
  key: XXXXXX,
  fieldList: [{
    field: 1,
    axis: 'T'
  }, {
    field: 2,
    axis: 'T'
  }, {
    field: 3,
    axis: 'T'
  }]
});
Weshahi
  • 23
  • 1
  • 10