Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the value of JSON object's key, changes other values also

Description: Have defined a JSON Object, as a global variable. 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 object (JSON_OBJECT) gets constructed by a function: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);
}
};

Problem Statement: Change the value of formula_type for user_id:123

Tried Below Code

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

Issue Facing: Above code changes the value of formula_type for user_id:123 and user_id:859

Resulting 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
        }
    ]
}
]

Help would be appreciated.

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);
like image 621
WEshruth Avatar asked Dec 17 '25 23:12

WEshruth


1 Answers

<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>
like image 50
Sanjay Kumar Avatar answered Dec 20 '25 15:12

Sanjay Kumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!