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?

4
  • 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? Commented Dec 11, 2020 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. Commented Dec 11, 2020 at 13:02
  • Yes Henning, thats what I am talking about. Commented Dec 11, 2020 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. Commented Dec 11, 2020 at 13:48

1 Answer 1

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.