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

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -