javascript - Efficient way to delete key of object in nested arrays -
my data structured this:
obj = { _id: "sjkd9skj", data: { dektop: [ { x: 2, y: 3, t: { key: 'aabbcc'} }, ... ], mobile: [ { x: 4, y: 3, t: { key: 'ffff'} }, ... ], print: [ { x: 7, y: 5, t: { key: 'ppp'} }, ... ] } }
in data, need remove t
keys array elements modes (desktop
,mobile
,print
).
what efficient way this? these mode arrays can quite large.
i have tried this:
obj.data.mobile.foreach((item)=>{ delete item.t; }); obj.data.desktop.foreach((item)=>{ delete item.t; }); obj.data.print.foreach((item)=>{ delete item.t; });
which not manipulate obj
in case, did not work. better suggestions?
i hope it's not blowing open door, can try reduce
aproach. immutability requirement fulfilled.
let obj = {_id:"sjkd9skj",data:{dektop:[{x:2,y:3,t:{key:'aabbcc'}},{x:2,y:3,t:{key:'aabbcc'}}],mobile:[{x:4,y:3,t:{key:'ffff'}},],print:[{x:7,y:5,t:{key:'ppp'}},]}}; const { _id } = obj; let newobj = object.keys(obj.data).reduce((s, a) => { obj.data[a].foreach((_, i) => { if (obj.data[a][i].t) { delete obj.data[a][i].t; } }) s.data[a] = obj.data[a]; return s; }, { _id, data: {} }); console.log(newobj);
Comments
Post a Comment