2017-03-09 47 views
0

我想將原始代碼(多變量w /循環)更改爲對象文字。是的,我知道你不能在一個對象字面上運行循環,但看到多個變量給我一個寵物peeve。修復代碼(多變量,循環和對象文字)

這裏的原代碼

var fruits, text, fLen, i; 
 

 
fruits = ["Banana", "Orange", "Apple", "Mango"]; 
 
fruits[6] = "Lemon"; // this is the code that got deleted 
 
fLen = fruits.length; 
 
text = "<ul>"; 
 
for (i = 0; i < fLen; i++) { 
 
    text += "<li>" + fruits[i] + "</li>"; 
 
    } 
 
text += "</ul>"; 
 
document.write(text);

而新代碼

var fruitOrder = { 
 
    fruit : ["Banana", "Orange", "Apple", "Mango"], 
 
    //fLen : fruits.length, --moved to for loop-- 
 
    text : "<ul>", 
 
    loop : function() { 
 
     for (var i = 0; i < this.fruit.length; i++) { 
 
     this.text += "<li>" + this.fruit[i] + "</li>"; 
 
     } 
 
     this.text += "</ul>"; 
 
     document.write(this.text); 
 
    } 
 
}; 
 
fruitOrder.loop();

無論是原&新的代碼工作,但問題是我在更改代碼時意外刪除了fruits[6] = "Lemon";。 我也試過很多辦法把它和讓它工作,但它不會給我我想要它返回的輸出:

  • 香蕉橘子蘋果芒果不確定不確定檸檬
+0

您可以添加循環函數對象 –

+0

@MaciejKozieja的原型是什麼有越來越'「檸檬」'到做:重新在返回對象之前修改水果對象'水果'陣列? – Barmar

+0

@Barmar沒有創建新的函數來evry object posible –

回答

0

如果你想要一個更遠的元素,你可以在數組裏面留下空白。

var fruitOrder = { 
 
    fruit : ["Banana", "Orange", "Apple", "Mango", , ,"Lemon"], 
 
    //fLen : fruits.length, --moved to for loop-- 
 
    text : "<ul>", 
 
    loop : function() { 
 
     for (var i = 0; i < this.fruit.length; i++) { 
 
     this.text += "<li>" + this.fruit[i] + "</li>"; 
 
     } 
 
     this.text += "</ul>"; 
 
     document.write(this.text); 
 
    } 
 
}; 
 
fruitOrder.loop();

或者你可以創建對象後添加到陣列。

var fruitOrder = { 
 
    fruit : ["Banana", "Orange", "Apple", "Mango"], 
 
    //fLen : fruits.length, --moved to for loop-- 
 
    text : "<ul>", 
 
    loop : function() { 
 
     for (var i = 0; i < this.fruit.length; i++) { 
 
     this.text += "<li>" + this.fruit[i] + "</li>"; 
 
     } 
 
     this.text += "</ul>"; 
 
     document.write(this.text); 
 
    } 
 
}; 
 
fruitOrder.fruit[6] = "Lemon"; 
 
fruitOrder.loop();

+0

downvoting幫助? upvoted。這裏有什麼錯在這裏願意爲300k :) –

+0

@sᴜʀᴇsʜᴀᴛᴛᴀ我不會讓它困擾我,我有充足的代表備用。 – Barmar

+0

因爲我希望能夠隨時將數字更改爲'fruits [6] =「檸檬」,所以我會拿第二個代碼;'謝謝! – lindsey

0
fruitOrder.fruit[6]= "Lemon"; 

會做

var fruitOrder = { 
 
    fruit : ["Banana", "Orange", "Apple", "Mango"], 
 
    //fLen : fruits.length, --moved to for loop-- 
 
    text : "<ul>", 
 
    loop : function() { 
 
     for (var i = 0; i < this.fruit.length; i++) { 
 
     this.text += "<li>" + this.fruit[i] + "</li>"; 
 
     } 
 
     this.text += "</ul>"; 
 
     document.write(this.text); 
 
    } 
 
}; 
 
fruitOrder.fruit[6]= "Lemon"; 
 
fruitOrder.loop();

0

如果你想Lemon永遠是在指數6,你可以使用JavaScript瓶蓋

var fruitOrder = (function() { 
 
    var data = { 
 
    fruit: ["Banana", "Orange", "Apple", "Mango"], 
 
    //fLen : fruits.length, --moved to for loop-- 
 
    text: "<ul>", 
 
    loop: function() { 
 
     for (var i = 0; i < this.fruit.length; i++) { 
 
     this.text += "<li>" + this.fruit[i] + "</li>"; 
 
     } 
 
     this.text += "</ul>"; 
 
     document.write(this.text); 
 
    } 
 
    }; 
 
    data.fruit[6] = "Lemon"; 
 
    return data; 
 
})(); 
 
fruitOrder.loop();

+0

但我希望能夠隨時將數字更改爲'fruits [6] =「Lemon」;',但感謝您的建議^。^ – lindsey

+0

@ user7021951這很有道理。感謝澄清。我更新了答案,以更符合這一點。 –