2

I have an object that looks like this

obj = [ {name: 'john', address:'123 whateverdr'}, {name: 'james', address:'123 whateverdr'}, {name: 'tom brady', address:'123 whateverdr'}]

I need to change the name to title case John or in the 3rd item Tom Brady needs to be title case;

Is there a one liner way that this can be done in typescript? The below doesnt work as it says obj.map is not a function

obj.map(d => d[0].toUpperCase() + d.substr(1));
          var wordArr = obj.map(m => m.split(" "));
          var newArr = wordArr.map(function (word) {
            word = word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
            obj = newArr;
          });
        }
Terrance Jackson
  • 606
  • 3
  • 13
  • 40
  • 2
    Have you tried anything yet ? – Nicolas Nov 08 '19 at 18:45
  • 2
    yea just added it – Terrance Jackson Nov 08 '19 at 18:46
  • [Iterate over array of objects](https://stackoverflow.com/q/11701628/215552); Loop through objects: [How do I loop through or enumerate a JavaScript object?](https://stackoverflow.com/q/684672/215552). Change string to title case: [Convert string to title case with JavaScript](https://stackoverflow.com/q/196972/215552) – Heretic Monkey Nov 08 '19 at 18:55

1 Answers1

2

var obj = [{
  name: 'john',
  address: '123 whateverdr'
}, {
  name: 'james',
  address: '123 whateverdr'
}, {
  name: 'tom brady',
  address: '123 whateverdr'
}];

var normal = obj.map(v => ({
  ...v,
  name: v.name
    .split(' ')
    .map(s => s.charAt(0).toUpperCase() + s.substring(1))
    .join(' ')
}))
console.log(normal);
Medet Tleukabiluly
  • 11,662
  • 3
  • 34
  • 69