Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript angularjs,需要转换对象属性_Javascript - Fatal编程技术网

Javascript angularjs,需要转换对象属性

Javascript angularjs,需要转换对象属性,javascript,Javascript,在我的angularjs应用程序中,我有一个模型,它是对象数组,如下所示: $scope.originalData = [{ "id": 10000, "transactionid": "gec43434", "status": "COMPLETED", "session_id": "TTYUU455667", "errors": "1", "start_timestamp": "2016-11-07 02:35:35",

在我的angularjs应用程序中,我有一个模型,它是对象数组,如下所示:

 $scope.originalData = [{

     "id": 10000,
     "transactionid": "gec43434",
     "status": "COMPLETED",
     "session_id": "TTYUU455667",
     "errors": "1",
     "start_timestamp": "2016-11-07 02:35:35",
     "log_field": "Sample Text For Testing"

 }, {

     "id": 10001,
     "transactionid": "r34fdfdf",
     "status": "COMPLETED",
     "session_id": "dfdfer3453df",
     "errors": "3",
     "start_timestamp": "2016-10-07 03:20:15",
     "log_field": "Sample Text2 For Testing"

 }];
现在,我需要修改相同的对象数组,使其如下所示,除了
log\u字段
其余所有属性都应位于同一对象的另一个属性下

$scope.modifiedData = [{
     "subItems": {
         "id": 1000,
         "transactionid": "gec43434",
         "status": "COMPLETED",
         "session_id": "TTYUU455667",
         "errors": "1",
         "start_timestamp": "2016-11-07 02:35:35"
     },
     "log_field": "Sample Text For Testing"

 }, {
     "subItems": {
         "id": 10001,
         "transactionid": "r34fdfdf",
         "status": "COMPLETED",
         "session_id": "dfdfer3453df",
         "errors": "3",
         "start_timestamp": "2016-10-07 03:20:15"
     },
     "log_field": "Sample Text2 For Testing"

 }]
看起来很简单

const originalData=[{“id”:10000,“transactionid”:“gec43434”,“status”:“COMPLETED”,“session_id”:“TTYUU455667”,“errors”:“1”,“start_timestamp”:“2016-11-07 02:35:35”,“log_field”:“用于测试的示例文本”},{“id”:10001,“transactionid”:“r34fdfdf”,“status”:“COMPLETED”,“session_id”:“dfer3453dff”,“errors”:“3”,“start_timestamp”:“2016-10-07 03:20:15”,“日志字段”:“用于测试的示例文本2”}]
const doNotMap='log\u字段'
常量modifiedData=originalData.map(d=>{
让子键=Object.keys(d).filter(key=>key!==doNotMap)
返回{
子项:子键。减少((对象,键)=>{
返回Object.assign(obj{
[键]:d[键]
})
},Object.create(null)),
[doNotMap]:d[doNotMap]
}
})
console.info('Modified=',modifiedData)
log('Original=',originalData)
似乎很简单

const originalData=[{“id”:10000,“transactionid”:“gec43434”,“status”:“COMPLETED”,“session_id”:“TTYUU455667”,“errors”:“1”,“start_timestamp”:“2016-11-07 02:35:35”,“log_field”:“用于测试的示例文本”},{“id”:10001,“transactionid”:“r34fdfdf”,“status”:“COMPLETED”,“session_id”:“dfer3453dff”,“errors”:“3”,“start_timestamp”:“2016-10-07 03:20:15”日志字段“:“用于测试的示例文本2”}]
const doNotMap='log\u字段'
常量modifiedData=originalData.map(d=>{
让子键=Object.keys(d).filter(key=>key!==doNotMap)
返回{
子项:子键。减少((对象,键)=>{
返回Object.assign(obj{
[键]:d[键]
})
},Object.create(null)),
[doNotMap]:d[doNotMap]
}
})
console.info('Modified=',modifiedData)

console.log('Original=',originalData)
我的方法将添加所有字段,而不管您的数据结构如何。如果您的字段需要更改,这是“略微”的未来证明。所有字段都将添加到
子项
,但
日志字段
除外

var data = [
  {
    "id": 10000,
    "transactionid": "gec43434",
    "status": "COMPLETED",
    "session_id": "TTYUU455667",
    "errors": "1",
    "start_timestamp": "2016-11-07 02:35:35",
    "log_field": "Sample Text For Testing"
  },
  {
    "id": 10001,
    "transactionid": "r34fdfdf",
    "status": "COMPLETED",
    "session_id": "dfdfer3453df",
    "errors": "3",
    "start_timestamp": "2016-10-07 03:20:15",
    "log_field": "Sample Text2 For Testing"
  }
];

var newData = [];

data.forEach(function(object) {

  // Create a new temporary object
  var temp = {
    subItems: {}
  };

  // Loop through the existing object
  for (var key in object) {

    // Leave the log field out of sub items
    if (key === "log_field") temp[key] = object[key];

    // Add all other keys/values to the subItems object
    temp["subItems"][key] = object[key];
  }

  // Push the new object to the newData array
  newData.push(temp);
});

console.log(newData);

以下是我的方法,它将添加所有字段,而不管您的数据结构如何。如果您的字段需要更改,这是“略微”的未来证明。所有字段都将添加到
子项
,但
日志字段
除外

var data = [
  {
    "id": 10000,
    "transactionid": "gec43434",
    "status": "COMPLETED",
    "session_id": "TTYUU455667",
    "errors": "1",
    "start_timestamp": "2016-11-07 02:35:35",
    "log_field": "Sample Text For Testing"
  },
  {
    "id": 10001,
    "transactionid": "r34fdfdf",
    "status": "COMPLETED",
    "session_id": "dfdfer3453df",
    "errors": "3",
    "start_timestamp": "2016-10-07 03:20:15",
    "log_field": "Sample Text2 For Testing"
  }
];

var newData = [];

data.forEach(function(object) {

  // Create a new temporary object
  var temp = {
    subItems: {}
  };

  // Loop through the existing object
  for (var key in object) {

    // Leave the log field out of sub items
    if (key === "log_field") temp[key] = object[key];

    // Add all other keys/values to the subItems object
    temp["subItems"][key] = object[key];
  }

  // Push the new object to the newData array
  newData.push(temp);
});

console.log(newData);

以下是您可以执行的操作。使用Array.map和delete

var originalData=[{
“id”:10000,
“交易ID”:“gec43434”,
“状态”:“已完成”,
“会话id”:“TTYU455667”,
“错误”:“1”,
“开始时间戳”:“2016-11-07 02:35:35”,
“日志字段”:“用于测试的示例文本”
}, {
“id”:10001,
“交易ID”:“R34FDF”,
“状态”:“已完成”,
“会话id”:“DFER3453DF”,
“错误”:“3”,
“开始时间戳”:“2016-10-07 03:20:15”,
“日志字段”:“用于测试的示例文本2”
}];
var result=originalData.map((项目)=>{
var log_字段=item.log_字段;
删除item.log_字段;
返回{
“子项”:第,
“日志字段”:日志字段
};
});

console.log(result);
以下是您可以执行的操作。使用Array.map和delete

var originalData=[{
“id”:10000,
“交易ID”:“gec43434”,
“状态”:“已完成”,
“会话id”:“TTYU455667”,
“错误”:“1”,
“开始时间戳”:“2016-11-07 02:35:35”,
“日志字段”:“用于测试的示例文本”
}, {
“id”:10001,
“交易ID”:“R34FDF”,
“状态”:“已完成”,
“会话id”:“DFER3453DF”,
“错误”:“3”,
“开始时间戳”:“2016-10-07 03:20:15”,
“日志字段”:“用于测试的示例文本2”
}];
var result=originalData.map((项目)=>{
var log_字段=item.log_字段;
删除item.log_字段;
返回{
“子项”:第,
“日志字段”:日志字段
};
});

console.log(结果)
答案应包含代码。指向外部网站的链接可能会补充您的答案,但重要部分应在StackOverflow上。指向外部网站的链接可能会补充您的答案,但重要部分应在StackOverflow上。这与AngularJS无关。这确实没有有什么关系AngularJS@Mahi答案是。现在很多浏览器都支持ES2015,巴贝尔负责REST。我无法对此进行投票。因为我不知道这是否正确wrong@PhilES2015很不错,但很难假设每个人都在使用它……我会坚持每个人都可以使用的常规JS答案。如果他们足够聪明,可以移植对于ES6,就这样吧。不,那不对。如果您更改了原始数组或其他内容,该怎么办。@SPlutCake不确定您的意思。ES2015是常规的JS@Mahi答案是。现在很多浏览器都支持ES2015,巴贝尔负责REST。我无法对此进行投票。因为我不知道这是否正确wrong@PhilES2015不错,但很难假设假设每个人都在使用它…我会坚持使用每个人都可以使用的常规JS答案。如果他们足够聪明,可以移植到ES6,那就这样吧。不,那不对。如果您更改原始阵列或其他东西会怎么样。@SPlutcake不确定您的意思。ES2015是常规JS