简体   繁体   中英

assigning one element from two arrays in another array

If I have two arrays and I want to get one element of each of them and put it in another array. I want something like this:

var array1 = [2, 3, 8];
var array2 = [4, 6, 5];
var array3 = array1[0].push(array2[0]);

so that the output would be: array3 = [2,4] . How can I do that in javascript ?

hint: I know that the push function is used to append element to array, but what I want is to append element from one array to another element from another array. Or, appending the two elements and putting it in array.

You could just create an array like

var array3 = [array1[0], array2[0]]

In your code there are multiple problems

  • array1[0] returns a number not a array so it doesn't have the push method
  • Even if you use array1.slice(0,1) to get an array with first element, the push() method will return an number(length of the array - 2) so the assignment won't work

so another way is

var array3 = array1.slice(0,1);
array3.push(array2[0])

I think you are looking for something like this:

var array3 = [];
    array3.push(array1[0]);
    array3.push(array2[0]);

I created an empty new array, and then added elements to it - easy as that!

You may also want to look at the array.splice function :)

This works with as many arrays as you want

function getArray (n) {
    var newArray = [];
    if (arguments[1][0] instanceof Array) {
        for (var i = 0; i < arguments[1].length; i += 1) {
            newArray.push(arguments[1][i][n]);
        }
    } else {
        for (var i = 1; i < arguments.length; i += 1) {
             newArray.push(arguments[i][n]);
        }
    }
    return newArray;
}

Lemme give you some examples to show you how this works:

getArray( index , array1 , array2 , so on... )

1:

> getArray(0, [1,2,3], [4,5,6])

[1,4]

2:

> getArray(1, [1,2,3], [4,5,6])

[2,5]

3:

> getArray(0, [1,2,3], [4,5,6], [7,8,9])

[1,4,7]

4:

 getArray(0, [ \n[1,2,3], \n[4,5,6] \n])  

[1,4]

Your way

According to your description, you can use this like:

var array1 = [1,2,3];
var array2 = [4,6,5];

var array3 = genArray(0, array1, array2);
jquery slice method is used to returns the array element which you want. for example:

   var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
   var a = ["fruit","veg"];
   var newarray = fruits.slice(0,1); 
   newarray.push(a[0])
   alert(newarray)

if you want to insert/delete method form array element use splice method. the major difference between : slice method : it return new array element without modified existing array element. it needs two arguments for start & EndIndex. For eg:

var a[3,4,5]
a.slice(0,1) // it returns [3]

splice method : Deletes and/or inserts elements in an array. Unlike slice(), the splice() method modifies the original array and returns a new array. it needs three arguments and, third argument is optional.The first parameter is the index to start deleting and/or insert elements, the second param is number of elements to remove and third param (optional) is the new elements to be added to the array.For eg:

 Var a = [1,2,5,6]
    var b = [3,4]
    a.splice().apply(a,[2,0].concat(b)) // a insert new element as [1,2,3,4,5,6]

//or if you want replace and append new element below like as:

 a.splice().apply(a,[2,1].concat(b)) // a delete [5] element and return as new array as [1,2,3,4,6] 

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