使用Object.assign更新对象而不是生产期望结果

时间:2018-12-09 20:38:31

标签: javascript object

我需要更新现有的对象数据

oldData = {
  "fnol": {
    "operationStatus": {
      "statusCode": "200",
      "statusMessage": "Operation was successful."
    },
    "data": {
      "policies": [
        {
          "formattedPolicyNumber": "abc8",
          "producerCode": "TP109"
        },
        {
          "formattedPolicyNumber": "abc8",
          "producerCode": "TP110"
        }
      ],
      "user": {
        "login": "URGENTNOTICES@test.COM",
        "firstName": "firsname",
        "lastName": "lastname",
        "dateOfBirth": "1960-01-01",
        "phone": {
          "number": "3242342342",
          "extension": "5454"
        }
      }
    }
  }
} 

需要更新

let editedData = {
    "fnol":{
    "data":{
        "user":{
                "phone":{"number":"","extension":""}
            }   
        }
    }
}

editedData = editedData.fnol.data.user.phone.number = '0000000000';

所以我正尝试使用

更新用户电话号码
newData = new Object.assign({}, oldData, editedData);

但是我还是要获取没有更新数字的oldData对象?

预先感谢

2 个答案:

答案 0 :(得分:2)

要获得预期结果,请使用以下选项

  1. 首先将oldData分配给editedData
  2. 更改editedData的电话号码
  3. 分配给newData

var oldData = {
  "fnol": {
    "operationStatus": {
      "statusCode": "200",
      "statusMessage": "Operation was successful."
    },
    "data": {
      "policies": [
        {
          "formattedPolicyNumber": "abc8",
          "producerCode": "TP109"
        },
        {
          "formattedPolicyNumber": "abc8",
          "producerCode": "TP110"
        }
      ],
      "user": {
        "login": "URGENTNOTICES@test.COM",
        "firstName": "firsname",
        "lastName": "lastname",
        "dateOfBirth": "1960-01-01",
        "phone": {
          "number": "3242342342",
          "extension": "5454"
        }
      }
    }
  }
} 

let editedData = Object.assign({}, oldData)


eeditedData = editedData.fnol.data.user.phone.number = '0000000000';

var newData = Object.assign({}, oldData, editedData);

console.log(newData);

codepen-https://codepen.io/nagasai/pen/QzWaXG?editors=1010

答案 1 :(得分:0)

您可以使用类似这样的函数来沿路径更改(或添加)值:

const oldData = {"fnol": {"data": {"policies": [{"formattedPolicyNumber": "abc8", "producerCode": "TP109"}, {"formattedPolicyNumber": "abc8", "producerCode": "TP110"}], "user": {"dateOfBirth": "1960-01-01", "firstName": "firsname", "lastName": "lastname", "login": "URGENTNOTICES@test.COM", "phone": {"extension": "5454", "number": "3242342342"}}}, "operationStatus": {"statusCode": "200", "statusMessage": "Operation was successful."}}}

const None = Symbol()

const setPath = ([ p = None, ...ps ]) => (val) => obj => 
  p === None
    ? { ...obj }
    : ps.length == 0
      ? { ...obj, [p]: val }
      : { ...obj, [p]: setPath (ps) (val) (obj[p]) }

const newData = setPath 
  (['fnol', 'data', 'user', 'phone', 'number']) 
  ('0000000000')
  (oldData)

console.log(newData)
.as-console-wrapper {height: 100vh !important}

这不会修改您的原始数据。它仅处理对象,而不处理数组。添加这些并不难。

如果您想使用.分隔的字符串而不是数组,则可以为该版本添加光泽,如下所示:

const setPathStr = (str) => setPath(str.split('.'))

const newData = setPathStr
  ('fnol.data.user.phone.number')
  ('0000000000')
  (oldData)

但是请注意,这样做的灵活性降低了。路径中的节点不能再包含.字符。这可能对您来说不是问题,但对于通用实用程序功能,这是一个真正的问题。

此功能从Ramda的assocPath函数中汲取了灵感。 (我是Ramda作家。)