2016-11-06 115 views
0

我想從javascript對象數組中刪除一些屬性。這是對象的數組。從javascript對象數組中刪除一些屬性

obj_array = [{ 
     "DATA_ID": 1, 
     "DATA_NAME": "Jim", 
     "DATA_BB_TYP": 2, 
     "DATA_MAC": "5474", 
    }, 
    { 
     "DATA_ID": 3, 
     "DATA_NAME": "Fro", 
     "DATA_BB_TYP": 33, 
     "DATA_MAC": "8e30", 
    }, 
    { 
     "DATA_ID": 2, 
     "DATA_NAME": "Jimb", 
     "DATA_BB_TYP": 2, 
     "DATA_MAC": "45e8", 
    }, 
    { 
     "DATA_ID": 4, 
     "DATA_NAME": "Kht1", 
     "DATA_BB_TYP": 35, 
     "DATA_MAC": "58d0", 
    }, 
    { 
     "DATA_ID": 6, 
     "DATA_NAME": "Sens", 
     "DATA_BB_TYP": 34, 
     "DATA_MAC": "d004", 
    } 
] 

我有這個字符串數組,指定要刪除的屬性。

var str_array_criteria = ["DATA_BB_TYP", "DATA_MAC"]; 

移除後,對象數組看起來像這樣;

obj_array_removed = [{ 
     "DATA_ID": 1, 
     "DATA_NAME": "Jim",   
    }, 
    { 
     "DATA_ID": 3, 
     "DATA_NAME": "Fro", 
    }, 
    { 
     "DATA_ID": 2, 
     "DATA_NAME": "Jimb", 
    }, 
    { 
     "DATA_ID": 4, 
     "DATA_NAME": "Kht1", 
    }, 
    { 
     "DATA_ID": 6, 
     "DATA_NAME": "Sens", 
    } 
] 

我正在使用node.js v6。

回答

2

在這裏你去,

var obj_array = [{ 
 
     "DATA_ID": 1, 
 
     "DATA_NAME": "Jim", 
 
     "DATA_BB_TYP": 2, 
 
     "DATA_MAC": "5474", 
 
    }, 
 
    { 
 
     "DATA_ID": 3, 
 
     "DATA_NAME": "Fro", 
 
     "DATA_BB_TYP": 33, 
 
     "DATA_MAC": "8e30", 
 
    }, 
 
    { 
 
     "DATA_ID": 2, 
 
     "DATA_NAME": "Jimb", 
 
     "DATA_BB_TYP": 2, 
 
     "DATA_MAC": "45e8", 
 
    }, 
 
    { 
 
     "DATA_ID": 4, 
 
     "DATA_NAME": "Kht1", 
 
     "DATA_BB_TYP": 35, 
 
     "DATA_MAC": "58d0", 
 
    }, 
 
    { 
 
     "DATA_ID": 6, 
 
     "DATA_NAME": "Sens", 
 
     "DATA_BB_TYP": 34, 
 
     "DATA_MAC": "d004", 
 
    } 
 
]; 
 

 
    var str_array_criteria = ["DATA_BB_TYP", "DATA_MAC"]; 
 

 
    var new_obj_array = obj_array.map(function(obj) { 
 
     str_array_criteria.forEach(function(prop) { 
 
     delete obj[prop]; 
 
     }); 
 
     
 
     return obj; 
 
    }); 
 

 
    console.log(new_obj_array);

+0

驚人。您可以回答的速度以及代碼的簡潔性。我應該瞭解地圖,過濾這些日子之一。不容易習慣他們。 – user781486

+1

@ user91579631,是的,他們很好玩的學習隊友:-) – Aruna

+0

是你的代碼功能編程?它看起來如此不同,但又精彩又短小。我是否必須學習FP來理解你的代碼? – user781486

相關問題