Pressing an array value on the first index

var Config = { Windows: ['apple','mangi','lemon'] } 

I have a condition and is based on the fact that I want to push the banana value in my array.

 If(Condition Passed) { Config.Windows.unshift('banana'); Windows: ['banana','apple','mangi','lemon'] Config.Windows.reverse(); // The way the Array elements are now reversed and First banana is accessed. } else { Config.Windows.reverse(); } 

This is not so ... when I use Config.Windows in my other function, there is no banana Value ... in general

 for each(var item in Config.Windows.reverse()) { Ti.API.info(item); //This does not print banana 
+7
source share
2 answers

There are many ways you can print a value to the beginning of an array. Immediately, I can think of two ways:

  • Create a new array and replace the old one

     if (condition) { Config.Windows = ['banana'].join(Config.Windows) Config.Windows.reverse(); } else { Config.Windows.reverse(); } 
  • Based on what you said, it would be wiser to always change the array and then push your value:

     //initial array: ['apple','mangi','lemon'] Config.Windows.reverse(); //['lemon','mangi','apple'] if (condition) { //this will get the same result as your code Config.Windows.push("banana"); //['lemon','mangi','apple', 'banana'] } 
+7
source

"Unshift is not supported by IE, so if your browser explains why it does not work."

http://msdn.microsoft.com/en-us/library/ie/ezk94dwt(v=vs.94).aspx

+5
source

All Articles