简体   繁体   English

对象值未正确更新

[英]object value not being updated correctly

I have an object as follows: 我有一个对象,如下所示:

{
"headerSection": {
    "text": "Some Text",
    "color": "red",
    "fontSize": "12px",
    "backgroundColor": "#000",
    "textAlign": "left"
   }
}

I have a dropdown list which users can choose a different font size ie 14px, 16px .. 20px . 我有一个下拉列表,用户可以选择其他字体大小,即14px, 16px .. 20px On the change event of this dropdown I want to change the value of fontSize in my above object so for that I do: 在此下拉列表的change事件中,我想更改上述对象中的fontSize值,因此我这样做:

$('#font-size-ddl').change(function () {
    var value = $(this).val();
    headerObj.fontSize = value;
    console.log(JSON.stringify(headerObj));
});

In the above console.log the output is: 在上面的console.log ,输出为:

{
"headerSection": {
    "text": "Some Text",
    "color": "red",
    "fontSize": "12px",
    "backgroundColor": "#000",
    "textAlign": "left"
 },
"fontSize": "20px",
}   

Can someone please tell me what I am doing wrong. 有人可以告诉我我在做什么错。

You should update headerSection fontSize property. 您应该更新headerSection fontSize属性。

In your case the compiler looking after fontSize property of your headerObj object, it don't found it and create a new property for your object. 在您的情况下,编译器会headerObj对象的fontSize属性,但找不到它,并为您的对象创建了一个新属性

 let headerObj={ "headerSection": { "text": "Some Text", "color": "red", "fontSize": "12px", "backgroundColor": "#000", "textAlign": "left" } } $('select').change(function () { var value = $(this).val(); headerObj.headerSection.fontSize = value; console.log(JSON.stringify(headerObj)); }).trigger('change'); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select> <option value="20px">20px</option> <option value="30px">30px</option> </select> 

I believe, after looking at your output, that the fontSize property is being improperly appended. 我相信,在查看您的输出后, fontSize属性被不正确地附加了。

You need to do this to add it under headerSection . 您需要执行此操作以将其添加到headerSection下。

$('#font-size-ddl').change(function () {
    var value = $(this).val();
    headerObj.headerSection.fontSize = value; // like this
    console.log(JSON.stringify(headerObj));
});
$('#font-size-ddl').change(function () { 
    var value = $(this).val(); 
    headerObj.headerSection.fontSize = value;
    console.log(JSON.stringify(headerObj));
 });

you're adding a value to headerObj instead of headerObj.headerSection, as you can see from the console.log 您正在将一个值添加到headerObj而不是headerObj.headerSection,如从console.log中看到的

You have a nested object. 您有一个嵌套的对象。 An unnested object would look like this 未嵌套的对象看起来像这样

{
"text": "Some Text",
"color": "red",
"fontSize": "12px",
"backgroundColor": "#000",
"textAlign": "left"
}

Instead you have an outside object, with a property called "headerSection" pointing at another object. 相反,您有一个外部对象,该对象具有一个称为“ headerSection”的属性,该属性指向另一个对象。 If you're not going to change your data structure, then you need to change your code to access the inner property. 如果您不打算更改数据结构,则需要更改代码以访问inner属性。 Something like this 像这样

$('#font-size-ddl').change(function () {
  var value = $(this).val();
  headerObj["headerSection"]["fontSize"] = value;
  console.log(JSON.stringify(headerObj));
});

Ok then you just need to make a little change. 好的,您只需要进行一些更改。

$('#font-size-ddl').change(function () {
    var value = $(this).val();
    headerObj.headerSection.fontSize = value;  // See here.
    console.log(JSON.stringify(headerObj));
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM