JavaScript and jquey are used to merge multiple arrays. javascriptjquey

Source: Internet
Author: User

JavaScript and jquey are used to merge multiple arrays. javascriptjquey

1. Native JavaScript implementation

var array1 = ["a", "b"];var array2 = ["c", "d"];

I want to merge the data into the following:

<span class="keyword" style="font-weight:bold">var</span> <span class="keyword" style="font-weight:bold">array</span>= [<span class="string" style="color:rgb(221,17,68)">"a"</span>, <span class="string" style="color:rgb(221,17,68)">"b"</span>, <span class="string" style="color:rgb(221,17,68)">"c"</span>, <span class="string" style="color:rgb(221,17,68)">"d"</span>];

Available

arr1.concat(arr2);

2. Jquery implementation

$.merge(array1,array2);console.log($.unique(array1));

How to merge and display two arrays in javascript

You wrote the error
Array = array1.concat (array2 );

TIPS: JavaScript 2D Array Operations

Window. onload = function () {var something = "b3"; var array = [arrA = ["a1", "a2", "a3", "a4 ", "a5"], arrB = ["b1", "b2", "b3"], arrC = ["c1", "c2", "c3", "c4 ", "c5", "c6"]; var tgtArr = ["with A", "with B", "with C"]; var outNum = 0; // The value of something does not belong to an array. The default output is "with A", for (var I = 0; I <array. length; I ++) {if (isContain (array [I], something) {outNum = I; break ;}} alert (tgtArr [outNum]);} // determine whether the obj array contains the value valuefunction isContain (obj, value) {for (var I = 0; I <obj. length; I ++) {if (obj [I] = value) {return true ;}} return false ;}

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.