在我的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"
}]
答案 0 :(得分:3)
这是你能做的。使用Array.map并删除。
select ir.ProductId,sum(ir.IssueQuantity)as 'Quantity',p.ProductName
--,ir.IssueDate,ir.ReturnDate
from Product.IssueReturn ir join Company.Product p on p.ProductId=ir.ProductId
group by ir.ProductId,p.ProductName
--,ir.IssueDate,ir.ReturnDate

万一,如果你不应该修改原始数据,这就是你可以做的。
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);

答案 1 :(得分:1)
似乎很简单
const 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"}]
const doNotMap = 'log_field'
const modifiedData = originalData.map(d => {
let subKeys = Object.keys(d).filter(key => key !== doNotMap)
return {
subItems: subKeys.reduce((obj, key) => {
return Object.assign(obj, {
[key]: d[key]
})
}, Object.create(null)),
[doNotMap]: d[doNotMap]
}
})
console.info('Modified =', modifiedData)
console.log('Original =', originalData)

所以这里的ES2015版本并没有太大差异
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 doNotMap = 'log_field'
var modifiedData = originalData.map(function(d) {
var subKeys = Object.keys(d).filter(function(key) {
return key !== doNotMap
})
var modified = Object.create(null)
modified.subItems = subKeys.reduce(function(obj, key) {
var keyObj = Object.create(null)
keyObj[key] = d[key]
return Object.assign(obj, keyObj)
}, Object.create(null))
modified[doNotMap] = d[doNotMap]
return modified
})
console.info('Modified =', modifiedData)
console.log('Original =', originalData)

答案 2 :(得分:1)
这是我的方法,无论您的数据结构如何,我都会添加所有字段。这是"略有"如果您的领域需要改变,未来的证据。除WebPack
外,所有字段都会添加到subItems
。
log_field