I have a JSON object that I want to change values dynamically with the object that comes from front end(ReactJs) which has same value as the key of that object.here is my JSON object.
var jsonObj = {
"fields": {
"field1": {
"key": "value_1"
},"field2": {
"name": "value_3"
},
"field3":{"accountId":"value_9"},
"field4": "value_2",
"field5": "value_4",
"field6": "value_5",
"field7": {
"value": "value_6"
},
"field8": {
"type": "doc",
"version": 1,
"content": [
{
"type": "paragraph",
"content": [
{
"text": "value_7",
"type": "text"
}
]
}
]
}
}
}
This is my other object that comes from React front-end
Obj1 {
value_4: '896',
value_9: '62cbef88ec233f24600c',
value_5: 'Software',
value_7: 'description from front',
value_6: 'test@gmail.com',
value_1: 'TRDSD',
value_2: 'Test sum',
value_8: '',
value_10: '',
value_11: '',
value_12: '',
value_13: '',
value_3: 'TRDSD_incident'
}
What I want is this,
{
"fields": {
"field1": {
"key": "TRDSD"
},"field2": {
"name": "TRDSD_incident"
},
"field3":{"accountId":"62cbef88ec233f24600c"},
"field4": "Test sum",
"field5": "896",
"field6": "Software",
"field7": {
"value": "test@gmail.com"
},
"field8": {
"type": "doc",
"version": 1,
"content": [
{
"type": "paragraph",
"content": [
{
"text": "description from front",
"type": "text"
}
]
}
]
}
}
}
Already I achieve this using this function.here what I do is first I take keys that have no nested objects into an array and check for each field.also I have same function for nested object keys and check for each in the same way( value,key,name,accountId). and finally I get the JSON object as I expected.
function usingTree2(obj2, map) {
function traverse2(children) {
for (let item of children) {
if (item && item.field4) {
item.field4 = map[item.field4];
}
if (item && item.field5) {
item.field5 = map[item.field5];
}
if (item && item.field6) {
item.field6 = map[item.field6];
}
if (item && item.field8.content[0].content[0].text) {
item.field8.content[0].content[0].text =
map[item.field8.content[0].content[0].text];
}
if (item && item.children) {
traverse2(item.children);
}
}
}
traverse2(obj2);
return obj2;
}
function usingTree(obj, map) {
function traverse(children) {
for (let item of children) {
if (item && item.key) {
item.key = map[item.key];
} else if (item && item.value) {
item.value = map[item.value];
} else if (item && item.name) {
item.name = map[item.name];
} else if (item && item.accountId) {
item.accountId = map[item.accountId];
} else if (item && item.id) {
item.id = map[item.id];
}
if (item && item.children) {
traverse(item.children);
}
}
}
traverse(obj);
return obj;
}
const map = obj1
var obj = Object.values(jsonObj.fields);
var obj2 = Object.values(jsonObj);
usingTree(obj, map);
usingTree2(obj2, map);
for (let i = 0; i < fieldCount; i ++) {
usingTree(obj, map);
}
for (let i = 0; i < fieldCount; i ++) {
usingTree2(obj2, map);
}
But the problem is fields in JSON object are changing. So instead of checking for each field how can I achieve this dynamically.Of course without using same function twice for nested objects. I know this is not a best way. I am new to Nodejs, so please help me to do this. Hope my question is clear. Thanks in advance.