Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Javascript Transformation Missing Objects

Hi I am transforming JSON

I have write the following program to add newName in the JSON array

If you take a look resultedEvents, categoryName and name is missing in the JSON.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const result2 = {
    "resultedEvents": [
        {
            "categoryName": "Football",
            "name": "Arsenal vs Aston Villa",
            "resultedMarkets": [
                {
                    "name": "Asian Handicap",
                },
                
      ]
    },
        {
            "categoryName": "Football",
            "name": "Arsenal vs Aston Villa (Live)",
            "resultedMarkets": [
                {
                    "name": "Asian Handicap -0.5 (1-3)",
        }
      ]
    }
    ]
}

const data2= result2.resultedEvents.map(items => {
    const newResult = items.resultedMarkets.map(function (item) {
      const newName = item.name.replace(/( -?\d+([.,]\d+)?)/g, "");
      return {...item,  newName};
    })
    return {  resultedMarkets: newResult  }
  }
);

console.log(data2)

Above program is giving me following

Output

[
    {
        "resultedMarkets": [
            {
                "name": "Asian Handicap",
                "newName": "Asian Handicap"
            }
        ]
    },
    {
        "resultedMarkets": [
            {
                "name": "Asian Handicap -0.5 (1-3)",
                "newName": "Asian Handicap (1-3)"
            }
        ]
    }
]

Expected

{
    "resultedEvents": [{
            "categoryName": "Football",
            "name": "Arsenal vs Aston Villa",
            "resultedMarkets": [{
                    "name": "Asian Handicap",
                    "newName": "Asian Handicap"
                }

            ]
        },
        {
            "categoryName": "Football",
            "name": "Arsenal vs Aston Villa (Live)",
            "resultedMarkets": [{
                "name": "Asian Handicap -0.5 (1-3)",
                "newName": "Asian Handicap (1-3)"
            }]
        }
    ]
}

Please help me to solve this issue I just want to add newName in the JSON rest should be as it is

>Solution :

you are returning only resultedMarkets in your outer map

just add ...items like this

const result2 = {
    "resultedEvents": [
        {
            "categoryName": "Football",
            "name": "Arsenal vs Aston Villa",
            "resultedMarkets": [
                {
                    "name": "Asian Handicap",
                },
                
      ]
    },
        {
            "categoryName": "Football",
            "name": "Arsenal vs Aston Villa (Live)",
            "resultedMarkets": [
                {
                    "name": "Asian Handicap -0.5 (1-3)",
        }
      ]
    }
    ]
}

const data2= result2.resultedEvents.map(items => {
    const newResult = items.resultedMarkets.map( item => {
      const newName = item.name.replace(/( -?\d+([.,]\d+)?)/g, "");
      return {...item,  newName};
    })
    return {...items,  resultedMarkets: newResult  }
  }
);

console.log(data2)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading