更改JSON对象键的值也会更改其他值

2022-07-21 00:00:00 json key-value jquery javascript

说明:我定义了一个JSON对象,作为全局变量。 var JSON_OBJECT = [];

[
{
    "user_id": "123",
    "AF": [
        {
            "formula_type": 0,
            "lag": 0
        }
    ],
    "Trend": [
        {
            "is_active": 0
        }
    ]
},
{
    "user_id": "859",
    "AF": [
        {
            "formula_type": 0,
            "lag": 0
        }
    ],
    "Trend": [
        {
            "is_active": 0
        }
    ]
}
]

JSON对象(JSON_OBJECT)由以下函数构造:jsonCreator

function jsonCreator() {

var af_Array = [];
var Trend_Array = [];
var selectedUsers = ['123','859','235']

for (var i = 0; i < 1; i++) {
    var af_keys = {}
    af_keys.formula_type = i;
    af_keys.lag = i;
    af_Array.push(af_keys);
}

for (var j = 0; j < 1; j++) {
    var trends_keys = {}
    trends_keys.is_active = j;
    Trend_Array.push(trends_keys);
}


for (var indexUsers = 0; indexUsers < selectedUsers.length; indexUsers++) {
    var jsonObj = {};
    jsonObj.user_id = selectedUsers[indexUsers]['rowId'];
    jsonObj.AF = af_Array;
    jsonObj.Trend = Trend_Array;
    JSON_OBJECT.push(jsonObj);
}
};

问题陈述:更改formula_type的值user_id:123

尝试以下代码

var currentUserID = '123';
var formulaType = 'FORMULA-1'

Object.keys(JSON_OBJECT).forEach(function(k) {
if (currentUserID == JSON_OBJECT[k]['user_id']) {
    JSON_OBJECT[k]['AF'][0]['formula_type'] = formulaType;
}
});

面临的问题: 上述代码更改了user_id:123user_id:859formula_type的值

结果JSON:

[
{
    "user_id": "123",
    "AF": [
        {
            "formula_type": 'FORMULA-1',
            "lag": 0
        }
    ],
    "Trend": [
        {
            "is_active": 0
        }
    ]
},
{
    "user_id": "859",
    "AF": [
        {
            "formula_type": 'FORMULA-1',
            "lag": 0
        }
    ],
    "Trend": [
        {
            "is_active": 0
        }
    ]
}
]

感谢您的帮助。

数据-lang="js"数据-隐藏="真"数据-控制台="真"数据-巴贝尔="假">
var JSON_OBJECT = [
{
    "user_id": "123",
    "AF": [
        {
            "formula_type": 0,
            "lag": 0
        }
    ],
    "Trend": [
        {
            "is_active": 0
        }
    ]
},
{
    "user_id": "859",
    "AF": [
        {
            "formula_type": 0,
            "lag": 0
        }
    ],
    "Trend": [
        {
            "is_active": 0
        }
    ]
}
];

var currentUserID = '123';
var formulaType = 'FORMULA-1'

Object.keys(JSON_OBJECT).forEach(function(k) {
  if (currentUserID == JSON_OBJECT[k]['user_id']) {
      JSON_OBJECT[k]['AF'][0]['formula_type'] = formulaType;
  }
});
console.log(JSON_OBJECT);


解决方案

问题在于JSON的创建,jsonCreator()。第jsonObj.AF = af_Array;行不是复制,它只是分配引用。

@ccarton的建议下找到了解决方案,将JSON_OBJECT转换为字符串并解析回来。

var currentUserID = '123';
var formulaType = 'FORMULA-1'

var JSON_OBJECT1 = JSON.stringify(JSON_OBJECT); 
JSON_OBJECT = JSON.parse(JSON_OBJECT1)

Object.keys(JSON_OBJECT).forEach(function(k) {
if (currentUserID == JSON_OBJECT[k]['user_id']) {
    JSON_OBJECT[k]['AF'][0]['formula_type'] = formulaType;
}
});

相关文章