![](/img/trans.png)
[英]How do I set a value as a parameter inside an object with plain javascript?
[英]How do I set a JavaScript object's value to null
我已經從數組創建了這個JS對象。
var rv = {};
$( ".part-name:visible" ).each(function( index ) {
//rv[$(this).text()] = arrayPartsName[$(this).text()];
rv[$(this).text()] = arrayPartsName[$(this).text()];
console.log(rv);
})
4GN: "4GN"
4GNTS: "4GNTS"
042645-00: "042645-00"
503711-03: "503711-03"
573699-05: "573699-05"
我必須將此對象與Materialize Autocomplete一起使用,並且必須對其進行編輯。 例如,正確的對象必須是這樣的
4GN: null
4GNTS: null
042645-00: null
503711-03: null
573699-05: null
怎么辦
從我的評論中挑選。 您可以將其設置為null
;)JavaScript是一種很酷的語言...您幾乎可以將任何對象的屬性設置為所需的任何內容, null
,特定值甚至是函數... 參見本主題的更多內容
但要專注於您的特定問題:
更改此行
rv[$(this).text()] = arrayPartsName[$(this).text()];
至
rv[$(this).text()] = null;
如果您在JSON對象中具有property
或鍵值,並且名稱中帶有短划線,則必須將其用引號"
括起來,否則將不會被視為有效。盡管這可能不那么明顯,但在示例中可能會出現問題您的密鑰是通過以下函數$(this).text()
。
var fruit = { "pear": null, // something null "talk": function() { console.log('WOOHOO!'); } // function } var apple = "app-le"; fruit[apple.toString()] = 'with a dash'; fruit["bana-na"] = 'with a dash'; // below is not allowed, the values will be evaluated as // properties that dont exist, and then your js will fail // fruit[pe-ar] = 'with a dash'; fruit.talk(); console.log(fruit);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.