更改JSON对象键的值,还更改其他值

时间:2018-06-25 11:34:32

标签: javascript jquery json key-value

说明:已将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的{​​{1}}的值更改为

尝试以下代码

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; } }); formula_type的{​​{1}}的值

生成的JSON

user_id:123

我们将不胜感激。

user_id:859

5 个答案:

答案 0 :(得分:3)

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    
        var currentUserID = '123';
		var formulaType = 'FORMULA-1';
        
        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 obj = jQuery.parseJSON(JSON_OBJECT);
       // var obj1 = jQuery.parseJSON(JSON_OBJECT);
$.each(obj, function(k,value) {
  if(value.user_id == currentUserID){
    JSON_OBJECT.AF = formulaType;
    obj[k]['AF'][0]['formula_type'] = formulaType;
  }
})	
		var myJSON = JSON.stringify(obj);	
        console.log(myJSON)
  
});
</script>

答案 1 :(得分:1)

下面的代码段有效:

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);

祝你好运!

答案 2 :(得分:1)

尝试以下操作:

if (JSON_OBJECT.length > 0) {
    JSON_OBJECT.forEach(function(obj, index) {
    if (obj.user_id == 123) {
        if (obj.hasOwnProperty('AF')) {
        obj.AF.forEach(function(obj, index) {
            if (obj.hasOwnProperty('formula_type')) {
            obj.formula_type = 'FORMULA-1';
          }
        });
        }
    }

  });
}

说明:JSON_OBJECT是对象数组。因此,我们需要先循环到数组中。

JSFiddle

答案 3 :(得分:1)

使用数组map()方法:

var jsonObj = [
{
    "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';

jsonObj.map(obj => {
  if (obj.user_id == currentUserID) {
    for (var i in obj["AF"]) {
      obj["AF"][i].formula_type = formulaType;
    }
  }
});

console.log(jsonObj);

在循环中使用JavaScript ...

var jsonObj = [
{
    "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';

for (var i in jsonObj) {
  if (jsonObj[i]["user_id"] == currentUserID) {
    for (var j in jsonObj[i]["AF"]) {
      jsonObj[i]["AF"][j].formula_type = formulaType;
    }
  }
}

console.log(jsonObj);

答案 4 :(得分:0)

  

问题出在创建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;
}
});