使用Object.assign将obj传递给obj

时间:2017-11-20 03:09:04

标签: javascript angularjs

所以我有一点点遭遇,我似乎无法知道这个问题是什么,因为我似乎无法让vm.dataUpdate.length给我一个未定义的值。

function massUpdate() {
  vm.dataUpdate = Object.assign({}, vm.leaveList);

  console.log(vm.dataUpdate.length); // returning undefined

  for (var x = 0; x < vm.dataUpdate.length; x++) {
    console.log(x);
    if (vm.dataUpdate[x].actionStatus === 'edited') {
      vm.dataUpdate[x].leaveStatus = vm.dataUpdate[x].action.actionName;
      console.log(vm.dataUpdate = vm.dataUpdate[x]);
    }
  }
}

修改

vm.leaveList

[
{
    "_id": "5a0e86e1cd39a911e3be8252",
    "comment": "\"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\"",
    "leaveSupervisor": "Barrack Obama",
    "leaveDays": "2",
    "leaveType": "Sick Leave",
    "fullName": "michelle obama",
    "toDate": "2017-11-17T16:00:00.000Z",
    "fromDate": "2017-11-16T16:00:00.000Z",
    "user": "admin123",
    "__v": 0,
    "leaveDateCreated": "2017-11-17T06:51:13.570Z",
    "leaveStatus": "Pending Approval"
},
{
    "_id": "5a0ea449cd39a911e3be8253",
    "comment": "my birthday",
    "leaveSupervisor": "Barrack Obama",
    "leaveDays": "1",
    "leaveType": "Birthday Leave",
    "fullName": "michelle obama",
    "toDate": "2017-11-20T16:00:00.000Z",
    "fromDate": "2017-11-20T16:00:00.000Z",
    "user": "admin123",
    "__v": 0,
    "leaveDateCreated": "2017-11-17T08:56:41.060Z",
    "leaveStatus": "Cancelled"
}]

欢迎任何建议。 感谢

1 个答案:

答案 0 :(得分:2)

您应该将数组传递给Object.assign。 请参阅以下代码:

            switch (fileExtension)
            {
                case ".xls": //Excel 97-03
                    excelConnectionString = ConfigurationManager.ConnectionStrings["Excel03ConString"]
                             .ConnectionString;
                    break;
                case ".xlsx": //Excel 07
                    excelConnectionString = ConfigurationManager.ConnectionStrings["Excel07ConString"]
                             .ConnectionString;
                    break;
            }

            excelConnectionString = String.Format(excelConnectionString, filePath, isHDR);

            //Create Connection to Excel work book and add oledb namespace
            OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
            excelConnection.Open();
            DataTable dt = new DataTable();

            dt = excelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            if (dt == null)
                return;

            String[] excelSheets = new String[dt.Rows.Count];
            int t = 0;
            //excel data saves in temp file here.
            foreach (DataRow row in dt.Rows)
            {
                excelSheets[t] = row["TABLE_NAME"].ToString();
                t++;
            }

            string query = string.Format("Select * from [{0}]", excelSheets[0]);//get the first worksheet(index = 0)
            using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, excelConnection))
            {
                //ds.ToString();
                dataAdapter.Fill(ds);

            }
            excelConnection.Close();
            dtUploadedChassisList = ds.Tables[0];

修改

因为您正在使用角度,您还可以尝试 angular.copy 它会对您的对象(在这种情况下为数组)进行深层复制,这样您就可以在更改属性时避免副作用vm.dataUpdate

var arr = [1,2,3]

var a = Object.assign({}, arr)
var b = Object.assign([], arr)

console.log(a) // { '0': 1, '1': 2, '2': 3 }
console.log(b) // [ 1 , 2 , 3 ]