gpt4 book ai didi

javascript - angularjs,需要转换对象属性

转载 作者:行者123 更新时间:2023-11-28 13:13:18 25 4
gpt4 key购买 nike

在我的 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_field 其余的所有属性应位于同一对象的另一个属性下。

$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"

}]

最佳答案

这是你可以做的。使用 Array.map 并删除。

var 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"
}];


var result = originalData.map((item) => {
var log_field = item.log_field;
delete item.log_field;
return {
"subItems": item,
"log_field": log_field
};
});

console.log(result);

以防万一,如果您不应该修改原始数据,您可以执行以下操作。

var 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"
}];


var result = originalData.map((item) => {
//Withou modifying original data.
var resultItem = Object.assign({}, item),
log_field = resultItem.log_field;
delete resultItem.log_field;

return {
"subItems": resultItem,
"log_field": log_field
};
});
console.log(originalData);
console.log(result);

关于javascript - angularjs,需要转换对象属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40757031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com