2

I have an array of gradients that I am trying to pass to a charting library each time a function is called.

The library requires the gradient to be passed in as an object in this format:

[0, 'rgba(73,115,214,.15)'],
[1, 'rgba(73,115,214,.07)']

I have built an array of gradients like so:

var gradients = [
  [
    ['rgba(73,115,214,.15)'],
    ['rgba(148,196,168,.1)']        
  ],
  [
    ['rgba(73,115,214,.07)'],
    ['rgba(41,189,102,.12)']
  ]
]

Each time a function is called, I store an index value, and need to retrieve the corresponding gradient in the array. How do I retrieve a specific index value from gradients and construct it into the necessary format?

var index = -1;
function setData() {
   index = index + 1;

   //I Need a gradient!
}

For instance, if the index was 2, I would want to return:

[0, 'rgba(73,115,214,.07)'],
[1, 'rgba(41,189,102,.12)']
2
  • Your question is not very clear, suppose your index is 3, do you expect [3, 'rgba(73,115,214,.07)'] to be returned ? Commented Jun 12, 2015 at 5:19
  • @JyotiPuri see edit to post. Commented Jun 12, 2015 at 5:27

3 Answers 3

1

Please check this fiddle for code: http://jsfiddle.net/qp6kL5ms/1/

You can write your loop like this:

function setData() {
   index = index + 1;
   var resultantGradient = [];
   gradients[index].forEach(function(gradient, index) {
      resultantGradient.push([index, gradient]);
   });
   return resultantGradient;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! As it turns out, the lib needs this to return resultantGradient inside brackets like so: [0,rgba(73,115,214,.15),1,rgba(148,196,168,.1)]. Am I able to place resultantGradient within the brackets or do I need to construct this as well?
0

Is this what you need? If you are preparing an hard coded data, do this.

var gradients = {
   "0" : "['rgba(73,115,214,.15)']",
   "1" : "['rgba(148,196,168,.1)']",
   "2" : "['rgba(73,115,214,.07)']",
   "3" : "['rgba(41,189,102,.12)']"
}

and while passing it you can prepare it by:

var globalArray = [];
for( var grad in gradients ) {
  var obj = [];
  var val = grad+","+gradients[grad];
  obj.push(val);
  globalArray.push(obj);
}

Now for your index thing which you mentioned, you can modify the function like this.

var globalArray = [];
// in your case you have show index as 2
function setData( index ){
   for( var grad in gradients ) {
       if ( grad < index ) {
         var obj = [];
         var val = grad+","+gradients[grad];
         obj.push(val);
         globalArray.push(obj);
       }
    }
}

Hope this helps

Comments

0

Is this what you expected?

var index = -1;
function setData() {
    var result = [];
    index = index + 1;

    //I Need a gradient!
    gradients[index].forEach(function(element, key) {
        result[key] = [key, element[0]];
    });
    console.log(result);
    return result;
}

By the way, array index starts from 0. So I expect if index==1, it will return:

[0, 'rgba(73,115,214,.07)'],
[1, 'rgba(41,189,102,.12)']

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.