jquery - Format Muilty dimentional array using javascript -
i json data showing below.
[ { "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" } ]
i need format json using javascript. arr[change_log_id][id]["column_name"] = column vale;
the idea use array.prototype.reduce()
follows.
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; }, []);
on data set produces following result:
[ , { '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' } } ]
Comments
Post a Comment