Add new key/value to a complex array [duplicate]

  arrays, javascript, json, react-native, reactjs

I receive an array from an API call like this:

{
    "message": "GOOD",
    "success": true,
    "Cat1": [
        {
            "idCadeau": 3,
            "Cadeau": "name2",
            "Unites": "4000"
        },
        {
            "idCadeau": 54,
            "Cadeau": "name4",
            "Unites": "1000"
        }
    ],
    "cat36": [
        {
            "idCadeau": 18,
            "Cadeau": "name15",
            "Unites": "12000"
        },
        {
            "idCadeau": 16,
            "Cadeau": "name14",
            "Unites": "11000"
        }
    ],
    "cat54": [
        {
            "idCadeau": 29,
            "Cadeau": "name41",
            "Unites": "60000"
        }
    ]
}

I would like to add a key/value to each object like this:
isSelect and styleClass

{
    "idCadeau": 54,
    "Cadeau": "name4",
    "Unites": "1000",

    "isSelect": true,
    "styleClass": "StylesGift.inValidChek"
}

I don’t know how to use map in this case, because it’s dynamic: cat1, cat36.. can change everytime I call the API I receive different cat..

also data.forEach.. return an error: data.forEach is not a function

any suggestions ?

Source: Ask Javascript Questions

LEAVE A COMMENT