所以我有一个数组,可以说:
var arr = ['apples' , 'cherries' , 'strawberries']
经过大量编码后,我很饿,所以我吃了'cherries'
所以现在我有
arr = ['apples',null,'strawberries']
所以我想知道是否有某种JavaScript函数可以返回数组中的第一个null元素?
例如,假设我的祖母给了我'potatoes'
,我想将它们添加到我有个好孩子的空空间中,并且我需要该空元素的索引,这样我才能知道在哪里放我奶奶的甜食。
答案 0 :(得分:1)
尝试使用以下代码删除元素:
TabsDetailProductViewController
并删除并添加try:
viewdidload
答案 1 :(得分:1)
arr.findIndex(element => element === null);
替换:
arr[arr.findIndex(element => element === null)] = "potatoes";
答案 2 :(得分:1)
由于from random import choice
import matplotlib.pyplot as plt
class RandomWalk():
def __init__(self, num_points=5000):
self.num_points = num_points
self.x_values = [0]
self.y_values = [0]
def fill_walk(self):
while len(self.x_values) < self.num_points:
x_direction = choice([1, -1])
x_distance = choice([0, 1, 2, 3, 4])
x_step = x_direction * x_distance
y_direction = choice([1, -1])
y_distance = choice([0, 1, 2, 3, 4])
y_step = y_direction * y_distance
if x_step == 0 and y_step == 0:
continue
next_x = self.x_values[-1] + x_step
next_y = self.y_values[-1] + y_step
self.x_values.append(next_x)
self.y_values.append(next_y)
rw = RandomWalk()
rw.fill_walk()
plt.scatter(rw.x_values, rw.y_values, s=15)
plt.show()
是一个falsy值,因此您可以简单地使用Array.findIndex并匹配虚假值:
null
答案 3 :(得分:0)
是的..我可以为此使用indexOf(null)...