使用spread运算符更新对象的多个属性

时间:2017-01-26 16:23:28

标签: javascript reactjs redux react-redux

我在reducer中有一个状态如下:

Error in invokeJava(isStatic = FALSE, objId$id, methodName, ...) : 
  org.apache.spark.SparkException: Job aborted due to stage failure: Task 0 in stage 1.0 failed 4 times, most recent failure: Lost task 0.3 in stage 1.0 (TID 4, 192.168.56.1): java.io.IOException: Cannot run program "Rscript":
CreateProcess error=2, Das System kann die angegebene Datei nicht finden
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
    at org.apache.spark.api.r.RRunner$.createRProcess(RRunner.scala:348)
    at org.apache.spark.api.r.RRunner$.createRWorker(RRunner.scala:386)
    at org.apache.spark.api.r.RRunner.compute(RRunner.scala:69)
    at org.apache.spark.api.r.BaseRRDD.compute(RRDD.scala:50)
    at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:319)
    at org.apache.spark.rdd.RDD.iterator(RDD.scala:283)
    at org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:38)
    at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:319)
    at org.apache.spark.rdd.RDD.iterator(RDD.scala:283)
    at org.apache.spark.rdd.MapPartitionsRDD.

我现在想要的是更新多个属性(timespan和customTimeSpan),就像这样(但这不起作用):

// The current source/selection
const selection = {
  timespan: "-3660",
  customTimespan: false,
  pathIds: [''],
  source: undefined,
  direction: 0,
  appClassIds: []
};

如何更新状态的多个属性?

2 个答案:

答案 0 :(得分:10)

您需要从那里删除额外的对象闭包

{ ...state, 
 timespan: action.timespan.value, 
 customTimespan: action.timespan.value
}

应该可以正常工作

如果你想在vanilla JS中这样做,你可以这样做:

Object.assign({}, state, { timespan: action.timespan.value, customTimespan: action.timespan.value})

我认为传播运营商更清洁,如果您有权访问它,应该走这条路线。

答案 1 :(得分:0)

您还可以使用散布运算符来实现相同目的,该运算符使您可以在对象中拥有新值,而不必对它们进行硬编码:

const selection = {
  timespan: "-3660",
  customTimespan: false,
  pathIds: [""],
  source: undefined,
  direction: 0,
  appClassIds: []
};

const newSelectionValues = {
  timespan: "-3600",
  customTimespan: true
};

const newSelection = { ...selection, ...newSelectionValues };