javascript - Change the casing title of the object inside an array - Stack Overflow

An array contains objects with property "title" which contains lower-casing text with _ . Nee

An array contains objects with property "title" which contains lower-casing text with _ . Need to change the title by splitting '_' and need to capitalize first letter after every space .

I can change all the title casing to upper case but I need only the first letter after space should be capitalized

  const listData = [
        {
            "title": "some_id",
            "dataTypes": "character varying(65535)"
        },
        {
            "title": "some_value",
            "dataTypes": "character varying(65535)"
        }
  ]
  const newData = []
  listData.map(el => newData.push({"title":el.title.toUpperCase().split('_').join(' '),"dataTypes" : el.dataTypes }))
  console.log(newData);

Expected :

   const newData = [
        {
            "title": "Some Id",
            "dataTypes": "character varying(65535)"
        },
        {
            "title": "Some Value",
            "dataTypes": "character varying(65535)"
        }
  ]

Actual :

  const newData = [ 
  { title: 'SOME ID' ,
   dataTypes: 'character varying(65535)' },
  { title: 'SOME VALUE' ,
   dataTypes: 'character varying(65535)' } ]

An array contains objects with property "title" which contains lower-casing text with _ . Need to change the title by splitting '_' and need to capitalize first letter after every space .

I can change all the title casing to upper case but I need only the first letter after space should be capitalized

  const listData = [
        {
            "title": "some_id",
            "dataTypes": "character varying(65535)"
        },
        {
            "title": "some_value",
            "dataTypes": "character varying(65535)"
        }
  ]
  const newData = []
  listData.map(el => newData.push({"title":el.title.toUpperCase().split('_').join(' '),"dataTypes" : el.dataTypes }))
  console.log(newData);

Expected :

   const newData = [
        {
            "title": "Some Id",
            "dataTypes": "character varying(65535)"
        },
        {
            "title": "Some Value",
            "dataTypes": "character varying(65535)"
        }
  ]

Actual :

  const newData = [ 
  { title: 'SOME ID' ,
   dataTypes: 'character varying(65535)' },
  { title: 'SOME VALUE' ,
   dataTypes: 'character varying(65535)' } ]
Share Improve this question asked Sep 6, 2019 at 4:29 RenJithRenJith 5571 gold badge5 silver badges18 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 5

You can do this:

const listData = [
  {
      "title": "some_id",
      "dataTypes": "character varying(65535)"
  },
  {
      "title": "some_value",
      "dataTypes": "character varying(65535)"
  }
]

const newData = []
listData.map(el => newData.push({
  "title":el.title.split('_').map( word => {
    return word[0].toUpperCase() + word.substring(1, word.length);
  }).join(' '),

  "dataTypes" : el.dataTypes }))
console.log(newData);

You will have to iterate through the split title and update the first letter.

The result is:

[ { title: 'Some Id', dataTypes: 'character varying(65535)' },
  { title: 'Some Value', dataTypes: 'character varying(65535)' } ]

One solution would be to implement a captilization function for a word as follows:

word => `${word.substring(0,1).toUpperCase()}${ word.substring(1)}`

This could be integrated with your existing code as shown below:

const listData = [{
    "title": "some_id",
    "dataTypes": "character varying(65535)"
  },
  {
    "title": "some_value",
    "dataTypes": "character varying(65535)"
  }
]

const result = listData.map(item => {
  
  return {
    dataTypes : item.dataTypes,
    title : item.title
        .split('_')
        .map(word => `${word.substring(0,1).toUpperCase()}${ word.substring(1)}`)
        .join(' ')
  }
  
});

console.log(result);

You were close. The are multiple ways you can capitalize words and one of the ways is using regex which will only capitalize letters.

"will 101skip that".replace(/\b[a-z]/g, match => match.toUpperCase());

const listData = [
        {
            "title": "some_id",
            "dataTypes": "character varying(65535)"
        },
        {
            "title": "some_value",
            "dataTypes": "character varying(65535)"
        }
  ]
  const newData = []
  listData.map(el => newData.push({"title":el.title.split('_').join(' ').replace(/\b[a-z]/g, match => match.toUpperCase()),"dataTypes" : el.dataTypes }))
  console.log(newData);

You can acplish what you are looking for with this...

let newData = listData.map(obj => {
    let newObj = {};
    for (let key in obj) {
        key === 'title' ? newObj[key] = [...obj[key].split('_').map(str => str.charAt(0).toUpperCase() + str.slice(1))].join(' ') : newObj[key] = obj[key];
    }
    return newObj;
});

...but I would prefer breaking up this code into smaller chunks. Here I have extracted the logic to split the string by the underscore and capitalize the end result into its own function.

function snakeCaseToCapitalize(snake_case) {
  return [...snake_case.split('_').map(str => str.charAt(0).toUpperCase() + str.slice(1))].join(' ');
};

let newData = listData.map(obj => {
  let newObj = {};
  for (let key in obj) {
    key === 'title' ? newObj[key] = snakeCaseToCapitalize(obj[key]) : newObj[key] = obj[key];
  }
  return newObj;
});

Hopefully that gets you where you want to go!

This uses regular expression to do what you need.

const listData = [
        {
            "title": "some_id",
            "dataTypes": "character varying(65535)"
        },
        {
            "title": "some_value",
            "dataTypes": "character varying(65535)"
        }
  ]
  const newData = [];
  listData.map(el => newData.push({"title":(el.title.split('_').join(' ').replace(/\b[a-z](?=[a-z]{1})/g, function(letter) {
return letter.toUpperCase(); } )),"dataTypes" : el.dataTypes }))
  console.log(newData);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744623255a4584478.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信