我有一個數組如下。Javascript數組修改值
var vehicle = new Array();
vehicle.push({ "name": "honda", "value": "civic" });
vehicle.push({ "name": "toyota", "value": "camry" });
我該如何修改這個數組來改變思域?
我有一個數組如下。Javascript數組修改值
var vehicle = new Array();
vehicle.push({ "name": "honda", "value": "civic" });
vehicle.push({ "name": "toyota", "value": "camry" });
我該如何修改這個數組來改變思域?
如果你不知道數組中的對象的位置,那麼你可以簡單地通過數組循環。找到該值後,將其替換並退出循環。如果數組中可能有多個「城市」值,則省略break;
語句,循環會將「城市」的所有實例更改爲「一致」。
for(var a = 0, len = vehicle.length; a < len; a++) {
if(vehicle[a].value === "civic") {
vehicle[a].value = "accord";
break;
}
}
試試這個
vehicle[0].value = 'accord';
而對於陣列完全
vehicle.map(function (vehicle) {
if (vehicle.value === 'civic') {
vehicle.value = 'accord';
}
});
嘗試vehicle[0].value = "accord"
只是這樣做:
vehicle[0].value = 'accord';
for (var i = 0, len = vehicle.length; i < len; i++) {
if (vehicle[i].value === "civic") {
vehicle[i].value = "accord";
}
}
vehicle[0]['value']='accord';
什麼是你的_Array_每個項目?一個通用的_Object_。所以你真的希望在_Object_中將'civic'修改爲'accord'。 –