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
?
所以输出是:array3 =[2,4]。如何用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.
提示:我知道push函数用于将元素添加到数组中,但是我想要的是将元素从一个数组添加到另一个数组中。或者,将两个元素附加到数组中。
4 个解决方案
#1
4
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 - array1[0]返回一个数字而不是数组,因此它没有push方法
- 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 - 即使使用array1.slice(0,1)获取具有第一个元素的数组,push()方法也会返回一个数字(数组的长度- 2),因此赋值将不起作用
so another way is
另一个方法是
var array3 = array1.slice(0,1);
array3.push(array2[0])
#2
1
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 :)
您可能还想查看数组。拼接功能:)
#3
0
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...
)
getArray(索引、array1 array2,等等…)
1:
>
getArray(0, [1,2,3], [4,5,6])
> getArray(0,(1、2、3),(4、5、6))
[1,4]
(1、4)
2:
>
getArray(1, [1,2,3], [4,5,6])
> getArray(1,(1、2、3),(4、5、6))
[2,5]
(2、5)
3:
>
getArray(0, [1,2,3], [4,5,6], [7,8,9])
> getArray(0, [1,2,3], [4,5,6], [7,8,9])
[1,4,7]
(1、4、7)
4:
getArray(0, [ [1,2,3], [4,5,6] ])
[1,4]
(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);
#4
0
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:
如果你想插入/删除方法表单数组元素使用splice方法。slice方法的主要区别在于:它返回新的数组元素而不修改现有的数组元素。它需要两个参数来启动和结束索引。如:
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:
splice方法:删除和/或插入数组中的元素。与slice()不同,splice()方法修改原始数组并返回一个新的数组。它需要三个参数,第三个参数是可选的。第一个参数是开始删除和/或插入元素的索引,第二个参数是要删除的元素数量,第三个参数(可选)是要添加到数组中的新元素。如:
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]
#1
4
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 - array1[0]返回一个数字而不是数组,因此它没有push方法
- 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 - 即使使用array1.slice(0,1)获取具有第一个元素的数组,push()方法也会返回一个数字(数组的长度- 2),因此赋值将不起作用
so another way is
另一个方法是
var array3 = array1.slice(0,1);
array3.push(array2[0])
#2
1
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 :)
您可能还想查看数组。拼接功能:)
#3
0
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...
)
getArray(索引、array1 array2,等等…)
1:
>
getArray(0, [1,2,3], [4,5,6])
> getArray(0,(1、2、3),(4、5、6))
[1,4]
(1、4)
2:
>
getArray(1, [1,2,3], [4,5,6])
> getArray(1,(1、2、3),(4、5、6))
[2,5]
(2、5)
3:
>
getArray(0, [1,2,3], [4,5,6], [7,8,9])
> getArray(0, [1,2,3], [4,5,6], [7,8,9])
[1,4,7]
(1、4、7)
4:
getArray(0, [ [1,2,3], [4,5,6] ])
[1,4]
(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);
#4
0
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:
如果你想插入/删除方法表单数组元素使用splice方法。slice方法的主要区别在于:它返回新的数组元素而不修改现有的数组元素。它需要两个参数来启动和结束索引。如:
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:
splice方法:删除和/或插入数组中的元素。与slice()不同,splice()方法修改原始数组并返回一个新的数组。它需要三个参数,第三个参数是可选的。第一个参数是开始删除和/或插入元素的索引,第二个参数是要删除的元素数量,第三个参数(可选)是要添加到数组中的新元素。如:
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]