JavaScript Array Partial programming (iv)

Source: Internet
Author: User
Tags javascript array

7 . Combine arrays arr1 and arrays arr2. do not directly modify the array arr, the result returns a new array

function concat (arr1, arr2) {return  arr1.concat (ARR2);}

or examine the Concat method without modifying the original array.

8, add an element item to the index of ARR. do not directly modify the array arr , resulting in the return of the new array  

function Insert (arr, item, index) {    var arrright=Arr.slice (index,arr.length);     var arrleft= arr.slice (0, index);     var arrm=return  arrm.concat (arrright);}

These questions sit down, summed up found that as long as the "Do not modify the array", the basic need to use the Concat method and slice method.

9, count the number of occurrences of an element with the value equal to item in arr  

 function   count (arr, item) { var  count=0;  for  (var  i=0;i<arr.length;i+ + item) {count  ++ return   count;}  

10. find the repeating element in the array arr

 function   duplicates (arr) { var  arrstr=arr.join ();     var   result;  for  (var  i=0;i<arr.length;i+ + if  (Arrstr.indexof (arr[i)                )!= Arrstr.lastindexof (Arr[i])) {Result.push (arr[i]); }}  return   R Esult;}  

Write your own method, the test result is, can find the duplicate elements, However, when displayed, all duplicates are displayed, as follows:

Obviously not, continue to improve!!

Just wait until I change it, and then come back and write.

JavaScript Array Partial programming (iv)

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.