2

I have this JSON below:

{
    "class": "List",
    "list": [{
            "name": "HandsUp"
            "schedule": {
                "type": "probability",
                "theme": "Regular",
                "occurance": {
                    "next": 1607687249008.9834,
                    "prev": null
                }
            }
        }, {
            "name": "Listing",
            "waitingScreenInfo": {
                "__class": "WaitingScreenInfo",
                "getRecapTime": 1607687753.7949834
            },
            "schedule": {
                "type": "Waiting2",
                "theme": "Listing",
                "occurance": {
                    "next": 1607687249008.9834,
                    "prev": null
                }
            }
        }
    ]
}

and I have this one:

{
    "HandsUp": "HandsDown",
    "Listing": "ImgList",
    "Waiting2": "UpNDown"
}

The equivalent of the strings in the first JSON are in the second JSON and I wonder how can I make a function that finds the equivalents of the strings in the first JSON and then replace all of them even though if there is more than one?

  • Do you want to replace the values in the first JSON that exist as properties in the second JSON with the values of the second one? – Henning Odén Dec 11 '20 at 12:26
  • So where do you need to replace any _keys_ then? That would be a bit more complex, than just replacing _values_. Your sample data suggests you might need only the latter here. – CBroe Dec 11 '20 at 13:02
  • Yes Henning, thats what I am talking about. – brian rockson Dec 11 '20 at 13:48
  • @CBore, sorry that I didn't show it in the first JSON. There were actually keys that had to be replaced too. – brian rockson Dec 11 '20 at 13:48

1 Answers1

3

You can do this

  • Go through the array
  • check if property name equals the key from your "repl" object
  • if yes reassign the value with the one in your "repl" object

let stru = {
    "class": "List",
    "list": [{
        "name": "HandsUp",
        "schedule": {
            "type": "probability",
            "theme": "Regular",
            "occurance": {
                "next": 1607687249008.9834,
                "prev": null
            }
        }
    }, {
        "name": "Listing",
        "waitingScreenInfo": {
            "__class": "WaitingScreenInfo",
            "getRecapTime": 1607687753.7949834
        },
        "schedule": {
            "type": "Waiting2",
            "theme": "Listing",
            "occurance": {
                "next": 1607687249008.9834,
                "prev": null
            }
        }
    }]
}

const repl = {
    "HandsUp": "HandsDown",
    "Listing": "ImgList",
    "Waiting2": "UpNDown"
}

console.log("Before ", stru)

stru.list.forEach(element => {
    // keys and values correspond at the index
    let keys = Object.keys(repl);
    let values = Object.values(repl);
    for (let i = 0; i < keys.length; i++) {
        if (keys[i] === element.name) {
            element.name = values[i];
        }
    }

});


console.log("Afterwards ", stru)
Aalexander
  • 4,987
  • 3
  • 11
  • 34