Rename the property names and change the values of multiple objects

This seems to do the trick:

function changeData(data){
    var title;
    for(var i = 0; i < data.length; i++){
        if(data[i].hasOwnProperty("thumb")){
            data[i]["thumbnail"] = data[i]["thumb"];
            delete data[i]["thumb"];
        }

        if(data[i].hasOwnProperty("title")){ //added missing closing parenthesis
            title = data[i].title;
            data[i].title="<span class="red">" + title + '</span>';
        }
    }
}

changeData(data);

EDIT:

I tried to make the function generic, but since you updated your answer to do very specific things, I’ve added the business logic to the function.

Leave a Comment