1
我有一個這樣的對象:JSON數組動態柱填充AngularJs
var myObject= [
{"name":'Tom', "city":'Chicago',"GroupCode":'1'},
{"name":'Harry', "city":'Wisconsin',"GroupCode":'1'},
{"name":'Rob', "city":'Los Angeles',"GroupCode":'2'},
{"name":'Peter', "city":'Seattle',"GroupCode":'2'},
{"name":'Dave', "city":'New York',"GroupCode":'3'},
{"name":'Steve', "city":'Boston',"GroupCode":'3}
];
變換爲
var myObject= [
{"name":'Tom', "city":'Chicago',"GroupCode":'1'},
{"name":'Harry', "city":'Wisconsin',"GroupCode":'1'},
{"name":'--', "city":'--',"GroupCode":'--'},
{"name":'Rob', "city":'Los Angeles',"GroupCode":'2'},
{"name":'Peter', "city":'Seattle',"GroupCode":'2'},
{"name":'--', "city":'--',"GroupCode":'--'},
{"name":'Dave', "city":'New York',"GroupCode":'3'},
{"name":'Steve', "city":'Boston',"GroupCode":'3}
];
使用以下代碼來在當過GroupCode正在改變之間添加新的記錄:
var gc = myObject[0].GroupCode;
myObject.forEach(function(ob, index) {
if (gc != ob.GroupCode) {
gc = ob.GroupCode;
//insert this object
myObject.splice(index, 0, {
"name": '--',
"city": '--',
"GroupCode": '--'
});
}
})
我如何使鑰匙/列在這裏動態?
myObject.splice(index, 0, {
"name": '--',
"city": '--',
"GroupCode": '--'
});
謝謝西里爾,這幫助了我。 – Srini