Implement data de-duplication in js

Source: Internet
Author: User

Today, Baidu also has
Today, a friend needs to write a simple txt file to repeat the js Code.

This code can process duplicate data in a txt file, provided that each row of the txt file is a data record.

When using this code, save the. html file with any name and your txt source file in a directory,

Run the HTML file and enter the name of the source TXT file (suffix does not need to be suffixed with. txt). Click "deduplication". In the dialog box that appears, select "yes ".

The program running information is displayed, and a test.txt file is generated under the directory.

For example:

Directory structure:

D:/test/src.txt source file

D:/test/test.html program file

D:/test/test.txt file generated

Code:

Source File: src.txt

13252491322132463
1323474226755

1323484945088
132340913294263
132377234213223
1323572633507
132592922813222
1323772329827
1325289426908
132529132280304
1323720690326
132871327636838
1323484945088
132522944132363
13238913228765

Process file test.html

<Html>
<Body>

<IE: Download ID = "oDownload1" STYLE = "behavior: url (# default # download)"/>
<Script>
Var my_array = new Array ();
Function onDownloadDone (downData ){
Document. getElementById ('showimport1'). innerHTML = downData;
}

Function txt2array (downData ){
My_array = downData. split ("/r/n ");
// Document. write ('after deduplication: '+ downData );
// CreateFile ();
}
Function CreateFile (tt)
{
Var fso, tf;
Fso = new ActiveXObject ("Scripting. FileSystemObject ");
Tf = fso. CreateTextFile ("test.txt", true );
Tf. WriteLine (tt );
Tf. Close ();
}
Array. prototype. distinct = function (){
Var a = [], B = [];
For (var prop in this ){
Var d = this [prop];
If (d = a [prop]) continue; // prevents loops to prototype
If (B [d]! = 1 ){
A. push (d );
B [d] = 1;
}
}
Return;
}
Function dosub (){
Var filname = document. getElementById ('txtid'). value;
ODownload1.startDownload(filname+'.txt ', txt2array );
Var test1 = my_array.distinct ();
Var getstr = test1.toString ();
Re =/,/g;
Var strtrans = getstr. replace (re, "/r/n ");
CreateFile (strtrans );
Document. getElementById ('show _ info'). innerHTML = "repeated successfully: <br/> Number of source file lines:" + my_array.length + "the number of lines removed is: '+ test1.length;
}

// Document. write ("deduplication successful: <br/> Number of source file lines:" + my_array.length + "the number of lines removed is: '+ test1.length );

// Document. write ('after deduplication: '+ my_array.distinct () + "</Script>
<Table width = "500" border = "0" align = "center" cellpadding = "0" cellspacing = "0">
<Tr onmouseover = "this. style. background = '# ffff'" onmouseout = "this. style. background ='' ">
<Td width = "120" align = "center"> <strong> description </strong> </td>
<Td align = "left"> <strong> txt file deduplication </strong> </td>
</Tr>
<Tr onmouseover = "this. style. background = '# ffff'" onmouseout = "this. style. background ='' ">
<Td align = "center"> source file name </td>
<Td align = "left"> <label> <input style = "width: 120px; "id =" txtid "name =" txtname "type =" text "value =" "/> </label>
<Font color = 'red'> file data must be stored by row </font> </td>
</Tr>
<Tr onmouseover = "this. style. background = '# ffff'" onmouseout = "this. style. background ='' ">
<Td width = "120" align = "center"> operation </td>
<Td align = "center"> www.2cto.com
<Input type = "button" name = "sub1" id = "sub1" value = "deduplication" onclick = "javascript: dosub ();"/>
</Td>
</Tr>
</Table>
<Div id = 'show _ info'> </div>
</Body>
</Html>


Author: zmyxmjz

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.