Search code examples
javascriptconcatenation

JavaScript: How do I concatenate two values from a single Object?


I know this is simple but for whatever reason I'm completely stumped right now.

I have an array of objects all with their own month and year properties and I'm trying to concatenate both from each object and then display them together. My data is as follows:

"caloriesBurned": [
        {
            "month": "Nov",
            "year": "2018",
            "calories": 64.15
        },
        {
            "month": "Dec",
            "year": "2018",
            "calories": 75.07
        }
]

Solution

  • you can do like this

    let arrayResult = []
    for(let i=0; i< caloriesBurned.length; i++){debugger
        test = i+ ":->" + caloriesBurned[i].month + " " + caloriesBurned[i].year;
        arrayResult.push(test)
    }
    

    Or if need to use ES6 then just use map function, then no need to use for loop instead of that use map as a high order function and then can achieve that in more easy way, if not convinient with that, then can use above code as well .