2017-04-07 54 views
-3

我得到如下所示的json數據。格式使用javascript的Muilty dimentional數組

[ 

{ 
     "id":5, 
     "entity_id":122, 
     "entity_type":"STUDENT", 
     "edit_type":"UPDATE", 
     "created_by":122, 
     "created_date":"2017-04-04T18:30:00.000Z", 
     "change_log_id":2, 
     "field_name":"full_name", 
     "current_value":"cur14111r name", 
     "previous_value":"old11111 name" 
    }, 
    { 
     "id":7, 
     "entity_id":122, 
     "entity_type":"STUDENT", 
     "edit_type":"UPDATE", 
     "created_by":122, 
     "created_date":"2017-04-04T18:30:00.000Z", 
     "change_log_id":2, 
     "field_name":"country", 
     "current_value":"Syriya11111", 
     "previous_value":"America1111" 
    }, 
    { 
     "id":6, 
     "entity_id":122, 
     "entity_type":"STUDENT", 
     "edit_type":"UPDATE", 
     "created_by":122, 
     "created_date":"2017-04-04T18:30:00.000Z", 
     "change_log_id":2, 
     "field_name":"school", 
     "current_value":"HMV11111", 
     "previous_value":"TMV11111" 
    }, 
    { 
     "id":8, 
     "entity_id":122, 
     "entity_type":"STUDENT", 
     "edit_type":"UPDATE", 
     "created_by":122, 
     "created_date":"2017-04-04T18:30:00.000Z", 
     "change_log_id":2, 
     "field_name":"nick_name", 
     "current_value":"Cokka1111", 
     "previous_value":"Kukka1111" 
    }, 
    { 
     "id":1, 
     "entity_id":122, 
     "entity_type":"STUDENT", 
     "edit_type":"UPDATE", 
     "created_by":122, 
     "created_date":"2017-04-05T23:38:08.000Z", 
     "change_log_id":1, 
     "field_name":"full_name", 
     "current_value":"curr name", 
     "previous_value":"old name" 
    }, 
    { 
     "id":3, 
     "entity_id":122, 
     "entity_type":"STUDENT", 
     "edit_type":"UPDATE", 
     "created_by":122, 
     "created_date":"2017-04-05T23:38:08.000Z", 
     "change_log_id":1, 
     "field_name":"country", 
     "current_value":"Syriya", 
     "previous_value":"America" 
    }, 
    { 
     "id":2, 
     "entity_id":122, 
     "entity_type":"STUDENT", 
     "edit_type":"UPDATE", 
     "created_by":122, 
     "created_date":"2017-04-05T23:38:08.000Z", 
     "change_log_id":1, 
     "field_name":"school", 
     "current_value":"HMV", 
     "previous_value":"TMV" 
    }, 
    { 
     "id":4, 
     "entity_id":122, 
     "entity_type":"STUDENT", 
     "edit_type":"UPDATE", 
     "created_by":122, 
     "created_date":"2017-04-05T23:38:08.000Z", 
     "change_log_id":1, 
     "field_name":"nick_name", 
     "current_value":"Cokka", 
     "previous_value":"Kukka" 
    } 
] 

我需要使用JavaScript. arr[change_log_id][id]["column_name"] = column vale;

+1

目前還不清楚你在問什麼。 – epascarello

+0

你從哪裏得到'column_name'? –

+0

你期待什麼? – Prasath

回答

0

的想法是使用Array.prototype.reduce()如下格式化這個JSON。

var input = [ ... ]; 

var output = input.reduce((acc, value) => { 
    acc[value.change_log_id] = acc[value.change_log_id] || {}; 
    acc[value.change_log_id][value.id] = acc[value.change_log_id][value.id] || {}; 
    acc[value.change_log_id][value.id][value.current_value] = value.current_value; 

    return acc; 
}, []); 

您的數據集也將產生以下結果:

[ , 
    { '1': { 'curr name': 'curr name' }, 
    '2': { HMV: 'HMV' }, 
    '3': { Syriya: 'Syriya' }, 
    '4': { Cokka: 'Cokka' } }, 
    { '5': { 'cur14111r name': 'cur14111r name' }, 
    '6': { HMV11111: 'HMV11111' }, 
    '7': { Syriya11111: 'Syriya11111' }, 
    '8': { Cokka1111: 'Cokka1111' } } ]