JavaScript Changing the value of JSON object's key, changes other values also change value json javascript,change value json python,change value

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);
Answer:1

<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>
Answer:2

Try following:

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

  });
}

Explanation: Here JSON_OBJECT is an array of objects. So we need to loop into the array first.

JSFiddle

Answer:3

The code snippet below works:

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);
Answer:4

Using Array map() method :

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);
Answer:5

I'm currently programming a webstie where the users can Upload .mp4 videos. I would like to make a thumbnail when the user uploads his video and write this image to a .jpg or .png file on my server. ...

I'm currently programming a webstie where the users can Upload .mp4 videos. I would like to make a thumbnail when the user uploads his video and write this image to a .jpg or .png file on my server. ...

  1. canvas save jpeg
  2. save canvas image jpeg

I've hit a snag after a few hours -- wondered if a fresh minded developer can just review the below, the code is simplied to show the problem. I am filtering a property value on an array of objects, ...

I've hit a snag after a few hours -- wondered if a fresh minded developer can just review the below, the code is simplied to show the problem. I am filtering a property value on an array of objects, ...

  1. filter array objects javascript
  2. filter array objects angular 6
  3. filter array objects
  4. filter array objects js
  5. filter array objects swift
  6. filter array objects jquery
  7. filter array objects angularjs
  8. filter array objects lodash
  9. filter array objects angular2
  10. array filter objects php
  11. filter array of objects by property
  12. filter array of objects typescript
  13. filter array of objects javascript es6
  14. filter array of objects by property javascript
  15. filter array of objects javascript by key
  16. filter array of objects by key
  17. filter array of objects by key value
  18. filter array of objects javascript by value
  19. filter array of objects based on property
  20. filter array of objects javascript based on property

I am new to Redux and i've been having a hard time rendering changes made to the store. I've been using Redux-DevTools to explore state changes and here is my problem. I have a sidebar which has ...

I am new to Redux and i've been having a hard time rendering changes made to the store. I've been using Redux-DevTools to explore state changes and here is my problem. I have a sidebar which has ...

  1. react redux component not updating
  2. react redux component not updating on state change
  3. react redux component lifecycle
  4. react redux component state
  5. react redux component example
  6. react redux component local state
  7. react redux component did mount
  8. react redux component communication
  9. react redux component dispatch
  10. react redux component not re rendering
  11. react redux component testing
  12. react redux component rendering twice
  13. react redux component
  14. react redux component will receive props
  15. react redux component container
  16. react redux component does not update
  17. react redux component did update
  18. react redux component vs container
  19. react redux component props
  20. react redux component library

I want to show pdf ajax response in browser.Problem is I can not directly open the url as pdf is created dynamically how can I show it. $.ajax({ url: url, type:...

I want to show pdf ajax response in browser.Problem is I can not directly open the url as pdf is created dynamically how can I show it. $.ajax({ url: url, type:...