如何在响应本机SetState中将数组推入多次状态?

时间:2019-05-05 04:34:11

标签: react-native setstate

我想将多个数组推到相同的状态(数组),如下所示。

    def ff(self,x):
        a1 = x
        z2 = np.dot(self.w1, a1) + self.b1
        a2 = sigmoid(z2)
        z3 = np.dot(self.w2, a2) + self.b2
        a3 = sigmoid(z3)
        return a1, z2, a2, z3, a3

def cost(self, x, y):
        # least squares cost function
        a3 = self.ff(x)[4]
        return np.sum(np.square(a3-y))


delta3 = (a3-y) * sigmoidd(z3)
delta2 = np.dot(self.w2.transpose(), delta3) * sigmoidd(z2)
db1 = delta2
db2 = delta3
dw2 = np.dot(delta3, a2.transpose())
dw1 = np.dot(delta2, a1.transpose())

# the following loops are for taking the 2 sided derivative of the cost function - these do not match up with the vectorized computations
        for i in range(0, dw1.shape[0]):
            for j in range(0, dw1.shape[1]):
                self.w1[i][j] += ep
                c_plus = self.cost(x, y)
                self.w1[i][j] -= 2*ep
                c_minus = self.cost(x,y)
                dw1[i][j] = (c_plus-c_minus)/(2*ep)
                self.w1[i][j] += ep

        for i in range(0, dw2.shape[0]):
            for j in range(0, dw2.shape[1]):
                self.w2[i][j] += ep
                c_plus = self.cost(x, y)
                self.w2[i][j] -= 2*ep
                c_minus = self.cost(x,y)
                dw2[i][j] = (c_plus-c_minus)/(2*ep)
                self.w2[i][j] += ep

        for i in range(0, db1.shape[0]):
            for j in range(0, db1.shape[1]):
                self.b1[i][j] += ep
                c_plus = self.cost(x, y)
                self.b1[i][j] -= 2*ep
                c_minus = self.cost(x,y)
                db1[i][j] = (c_plus-c_minus)/(2*ep)
                self.b1[i][j] += ep
        for i in range(0, db2.shape[0]):
            for j in range(0, db2.shape[1]):
                self.b2[i][j] += ep
                c_plus = self.cost(x, y)
                self.b2[i][j] -= 2*ep
                c_minus = self.cost(x,y)
                db2[i][j] = (c_plus-c_minus)/(2*ep)
                self.b2[i][j] += ep
        return dw1, dw2, db1, db2

上面的代码只是一个例子。

在我的项目中,我导入6个数组,其中包含来自不同商店的对象。 我想将所有这些数组推到相同的状态(在本例中为itemsArrayMixed)。

当然,如果我只想处理一个数组,那很容易处理,我知道该怎么做。

但是,我不知道如何将多个数组推到相同的状态...

1 个答案:

答案 0 :(得分:0)

您只是在混合数组中添加了新数组(itemsArray1),而没有获得其先前的值。您应该尝试一下,它将起作用

handleSetState1() {
  let itemArray1 = [
    {name: 'aaa', id: '001', address: 'xxx' },
    {name: 'aaa', id: '002', address: 'yyy'},
    {name: 'ccc', id: '003', address: 'zzz'},
   ]
  this.setState({ 
   itemsArray1 
  });
  this.setState({ itemsArrayMixed: [...this.state.itemsArrayMixed, itemArray1] });
}

这将更新itemArrayMixed并存储所有值。