JS how to go heavy and string odd digit lowercase to capital JS to go heavy and string odd bit lowercase capitalization tutorial

Source: Internet
Author: User
Tags alphabetic character array to string lowercase regular expression

In the interview, we often go to the array for weight. As a qualified front-end developer, it is not known that several ways to go heavy are not in the should. Not much nonsense to say directly open the ...

One, IndexOf () method

Implementation idea: Use the indexof () method to determine if there is a value in the new array, and if not, push the value into the array

The code is as follows Copy Code
First define an array
Vararr = [, ', ', ',,,,,,,,];
Functionremov (Array) {
Vararr=[];
for (vari=,len=array.length;i<len;i++) {
if (Arr.indexof (Array[i]) ==-) {
Arr.push (Array[i])
}
}
Returnarr;
}
Console.log (Remov (arr));//[, "", "" ",,,,,,,,

The code is very simple, stating that a little indexof () is ES5 in the method, incompatible IE8, so use the prior consideration of compatibility issues.

Second, sort () + to determine the adjacent two numbers

The idea: First use the sort () method of the array to sort the array in ascending order, then judge whether the adjacent two numbers are equal, and if not equal, push the preceding numbers into the newly created array

The code is as follows Copy Code
Vararr = [, ', ', ',,,,,,,,];
Functionremov (Array) {
Varlinarr = [];
Array.Sort ();
for (Vari =, Len = Array.Length i < len; i++) {
if (array[i +]!== Array[i]) {
Linarr.push (Array[i])
}
}
Returnlinarr;
}
Console.log (Remov (arr));//["",,,,, "",,,,]

Third, create an empty object and an empty array

Implementation idea: In the For loop, determine if there is a current item in the object, and if not, push the current item into the newly created array, assigning the current item as a property of the object to 1

The code is as follows Copy Code
Vararr = [, ', ', ',,,,,,,,];
Functionremov (array) {
    varres = [];
    varobj={};
    for (vari=;i<array.length;i++) {
   & Nbsp;  if (!obj[array[i]]) {
        res.push (arra Y[i]);
        obj[array[i]]=;
      }
    
    returnres;
  
  console.log (Remov (arr));//[, "", "",,,,,,,

As for why the current item is assigned a property of 1 as an object, for example, 5 in an array, there is no 5 in the first object, we push it into the new array, and if we do not assign the 5 attribute, the next time we detect the second 5 o'clock object there is no push to the new array. So here you assign the current item as a property of object 1, and of course you can assign it to another value. That's a little. What do you do? Meng Silk tip month complain? hellip;

Everyone has to notice the third method of the string ' 4 ' and the value 4 here's a little bit of a disappointment.

The third method used for reference by this method and the third method

Implementation of the idea: for the loop to determine whether there is a current item in the object, if not the current item as a property of the object assigned to 1, otherwise the current item + +. Iterate through the properties in the object after the for loop completes

The code is as follows Copy Code
Vararr = [, ', ', ',,,,,,,,];
Functionremov (Array) {
Varobj = {};
Varres = [];
for (vari=;i<array.length;i++) {
if (!obj[array[i]]) {
obj[array[i]]=;
}else{
obj[array[i]]++;
}
}
for (Varkinobj) {
Res.push (k);//b. Here, push the object properties that are traversed into the newly created array
Console.log (k)//a. Print the properties of the traversed object directly
}
Returnres;//b. This returns the newly created array
}
Console.log (Remov (arr)); This prints out the results of the Remov (arr) function ["," "," "," "," "," "", "", "" "," "" "" "," "" "" "," "" "
Remov (arr);//a

There is a small doubt in this approach: the annotation contains a is the traversal of the object attributes directly printed out the results of the following figure

The b in the annotation is the push of the properties of the traversed object into the newly created array, and the resulting printed array is a string.

Five, two for loops to traverse

Implementation of the idea: This method because of the use of a lot of it, create an empty array, the first for the loop to save an array value in the variable item, in the second for loop in the new array of values and variables, if equal to break, When the second for loop is finished, it is judged whether the length of the new array is equal to the J variable in the second loop and, if equal, adds the item in the first loop to the new array

The code is as follows Copy Code
Vararr = [, ', ', ',,,,,,,,];
Functionremov (Array) {
Varres = [];
for (vari=,len=array.length;i<len;i++) {
Varitem = Array[i];
for (varj=;j<res.length;j++) {
if (Res[j]==item) {
Break
}
}
if (res.length==j) {
Res.push (item);
}
}
Returnres;
}
Console.log (Remov (arr));//[, "", "",,,,,,,,]

Of course there are a lot of other array to weight method, here is sorted so much, there are errors or improper place please point out, we discuss learning together.

Add an interview question:

Topic Description: Enter a string of strings from 1 to start numbering, if you encounter odd lowercase English letters, then converted to the corresponding uppercase letters. If you encounter a non-English alphabetic character, do not count the odd even number, but the result is to retain the character

  code is as follows copy code
Varstr = ' sdt$^uyggtdgabn ';
document.write (str+ "<br/>");//Output the original string to the document
The varj=,reg=/[a-za-z]/;//j variable is used to count and define a regular expression to determine whether it is a letter
Varsarr = Str.split ("");//convert String to array
for (vari=;i<sarr.length;i++) {
if (Reg.test (Sarr[i])) {//If the letter J + +
j+=;
if (j%==) {//If an odd number converts the current item to uppercase
Sarr[i] = Sarr[i].touppercase ();
}
}
}
str = Sarr.join (');//convert array to string
document.write (str);//output the converted string to the document

Attach a string contrast diagram before and after conversion

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.