[英]Pushing Array value to first Index
var Config = {
Windows: ['apple','mangi','lemon']
}
I have a condition and based on 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();
}
It does not do it... when i use the Config.Windows in my other function there is no banana
Value... at all 它不会这样做......当我在我的其他功能中使用Config.Windows时,根本就没有
banana
价值......
for each(var item in Config.Windows.reverse()) {
Ti.API.info(item);
//This does not print banana
There are many ways in which you can push the value to the front of the 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've said, it would make more sense to always reverse the array, 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'] }
"The unshift is not supported by IE so if that's your browser it explains why it's not working. " “IE不支持unshift,所以如果这是你的浏览器,它解释了为什么它不起作用。”
http://msdn.microsoft.com/en-us/library/ie/ezk94dwt(v=vs.94).aspx http://msdn.microsoft.com/en-us/library/ie/ezk94dwt(v=vs.94).aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.