简体   繁体   中英

Capitalize second item in an array

I want to capitalize the second word in the array, I tried it based on the index number, however I get undefined for array[i+1] . Is there another way?

const filteredSegments = segments.map((segment, i, array) => {
    const hide = i === 0 && new RegExp(namePatterns.join('|'), 'i').test(segment.words)
    const next = array[i + 1]

    if (hide && next) { next.words.toUpperCase() }
    return { ...segment, words: hide ? segment.words.replace(/./g, '\u200c') : segment.words }
})

Counts even and odd in the loop and changes every second record

 var arr = ['lorem', 'ipsum', 'dolor', 'sit', 'amet', 'consectetur', 'adipisicing', 'elit', 'esse', 'voluptatibus', 'illum', 'fuga', 'quae', 'consequatur', 'pariatur'] for (var i = 0; i < arr.length; i++) { if (i % 2 !== 0) { arr[i] = arr[i].charAt(0).toUpperCase() + arr[i].slice(1); } } console.log(arr);

Second option: Another way to solve the task is if we loop the array through two values

 var arr = ['lorem', 'ipsum', 'dolor', 'sit', 'amet', 'consectetur', 'adipisicing', 'elit', 'esse', 'voluptatibus', 'illum', 'fuga', 'quae', 'consequatur', 'pariatur'] for (var i = 1; i < arr.length; i += 2) { arr[i] = arr[i].charAt(0).toUpperCase() + arr[i].slice(1); } console.log(arr);

Answer the question from the comment below the post:

("I want to capitalize only the second word if the first is empty string")

The code checks if the first string in the array is empty and if there is a second string if the condition is true... it performs the task of the second.

If there is a first string, it performs the task of the first -> (If you don't need it this, just remove the else )

 var arr = ['', 'ipsum', 'dolor', 'sit', 'amet', 'consectetur', 'adipisicing', 'elit', 'esse', 'voluptatibus', 'illum', 'fuga', 'quae', 'consequatur', 'pariatur'] if (arr[0].length === 0 && arr.length > 1) { arr[1] = arr[1].charAt(0).toUpperCase() + arr[1].slice(1); } else { arr[0] = arr[0].charAt(0).toUpperCase() + arr[0].slice(1); } console.log(arr);

Just the second item?

 function capitalizeSecondItem(arr) { if (arr.length > 1) { arr[1] = arr[1].charAt(0).toUpperCase() + arr[1].slice(1); } return arr; } console.log(capitalizeSecondItem(['hello', 'world'])); console.log(capitalizeSecondItem(['foo'])); console.log(capitalizeSecondItem([])); console.log(capitalizeSecondItem(['the', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']));

Capitalizing only second Word, for that, we don't have to iterate the array, Just check it array is bigger than or equal to length 2 and if yes then just capitalize the second word and keep remaining element as it is, its more time efficient too as we just have to perform on lookup in one certain index rather than iterating the whole array:

 let segment = ["", "to", "capitalize", "the","second","word","in","the","array"]; if(segment.length >=2){ segment[1] = segment[1][0].toUpperCase() + segment[1].substr(1); } console.log(segment );

Capitalize second Word if first element is empty string:

 let segment = [ "", "want", "to", "capitalize", "the", "second", "word", "in", "t", "array", ]; let sentense = segment.join(" ").trim(); sentense = sentense[0].toUpperCase() + sentense.substr(1); console.log(sentense.split(" "));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM