使用其他Array元素填充每个null元素

时间:2022-01-21 19:37:15

I'm facing a hard time understanding arrays. I have two of them, which I want to pick one and populate null elements with the other array elements. I currently have the below:

我很难理解数组。我有两个,我想选择一个并用其他数组元素填充null元素。我目前有以下内容:

var arr1 = [1,2,3,4]
var arr2 = [null, 99, null, null]

arr2.map((item) => {
  if (item == null) {
    arr2 = arr1.splice(item, 1)
  }
})

console.log(arr2)
// is outputing [3]. Expected is 1, 99, 3, 4

I'm using splice as I see it as the most close method I can use to accomplish my goal. Also, .map is used but not a requirement.

我正在使用拼接,因为我认为它是我用来实现目标的最接近的方法。此外,使用.map但不是必需的。

I'll happy visit any related/already answered question, but since English is not my main language, I couldn't find any related question.

我很高兴访问任何相关/已经回答的问题,但由于英语不是我的主要语言,我找不到任何相关的问题。

4 个解决方案

#1


1  

Your code is going to call splice(null, 1) on arr1 and assign the result on top of the entire arr2 three times.

您的代码将在arr1上调用splice(null,1)并将结果分配给整个arr2三次。

map is designed to allow you to go through each item in an array and provide a substitute for each item, but you need to use it correctly, like this:

map旨在允许您遍历数组中的每个项目并提供每个项目的替代项,但您需要正确使用它,如下所示:

var arr1 = [1,2,3,4]
var arr2 = [null, 99, null, null]

arr2 = arr2.map((item, i) => item === null ? arr1[i] : item )

console.log(arr2)

#2


1  

Just avoid the splice, and take the value you need with a ternary operator:

只需避免拼接,并使用三元运算符获取所需的值:

var arr1 = [1,2,3,4]
var arr2 = [null, 99, null, null]

arr2 = arr2.map( (item, i) => item === null ? arr1[i] : item );

console.log(arr2)

#3


0  

You are on the right track, but the callback in map is supposed to return the element to be used in the new array. Try this instead.

您处于正确的轨道上,但是map中的回调应该返回要在新数组中使用的元素。试试这个。

arr2 = arr2.map((value, index) =>
    value != null ? value : arr1[index]
});

#4


0  

If you know that arr1 and arr2 have same size you could do

如果您知道arr1和arr2具有相同的大小,那么您可以这样做

for(var i=0;i<arr2.length;i++){
  arr2[i]= arr2[i] || arr1[i];
}

I am using the for loop because it's ideal if you're just beginning working with arrays in order to fully understand how they work. The || operator, also known as null-coalescing operator, returns the first truthy operand to the assignment.

我正在使用for循环,因为如果你刚刚开始使用数组以便完全理解它们是如何工作的,那么它是理想的。 ||运算符,也称为null-coalescing运算符,将第一个truthy操作数返回给赋值。

#1


1  

Your code is going to call splice(null, 1) on arr1 and assign the result on top of the entire arr2 three times.

您的代码将在arr1上调用splice(null,1)并将结果分配给整个arr2三次。

map is designed to allow you to go through each item in an array and provide a substitute for each item, but you need to use it correctly, like this:

map旨在允许您遍历数组中的每个项目并提供每个项目的替代项,但您需要正确使用它,如下所示:

var arr1 = [1,2,3,4]
var arr2 = [null, 99, null, null]

arr2 = arr2.map((item, i) => item === null ? arr1[i] : item )

console.log(arr2)

#2


1  

Just avoid the splice, and take the value you need with a ternary operator:

只需避免拼接,并使用三元运算符获取所需的值:

var arr1 = [1,2,3,4]
var arr2 = [null, 99, null, null]

arr2 = arr2.map( (item, i) => item === null ? arr1[i] : item );

console.log(arr2)

#3


0  

You are on the right track, but the callback in map is supposed to return the element to be used in the new array. Try this instead.

您处于正确的轨道上,但是map中的回调应该返回要在新数组中使用的元素。试试这个。

arr2 = arr2.map((value, index) =>
    value != null ? value : arr1[index]
});

#4


0  

If you know that arr1 and arr2 have same size you could do

如果您知道arr1和arr2具有相同的大小,那么您可以这样做

for(var i=0;i<arr2.length;i++){
  arr2[i]= arr2[i] || arr1[i];
}

I am using the for loop because it's ideal if you're just beginning working with arrays in order to fully understand how they work. The || operator, also known as null-coalescing operator, returns the first truthy operand to the assignment.

我正在使用for循环,因为如果你刚刚开始使用数组以便完全理解它们是如何工作的,那么它是理想的。 ||运算符,也称为null-coalescing运算符,将第一个truthy操作数返回给赋值。

相关文章