Home > Software engineering >  Javascript Transformation Missing Objects
Javascript Transformation Missing Objects

Time:05-06

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.

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

CodePudding user response:

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)

  • Related