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;
          });
        }
3

1 Answer 1

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);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.