1

My data is structured like 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, I need to remove all t keys from all array elements for all modes (desktop,mobile,print).

What would be the most efficient way to do this? These mode arrays can get quite large.

I have tried it like 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 does actually not manipulate the obj in this case, so it did not work. Any better suggestions?

4
  • I can't reproduce the issue ..? ts are removed ... Commented Jul 25, 2017 at 11:36
  • Thanks @Kinduser I'm seeing this issue in my app as I'm passing states to be manipulated in redux. It must be related then to a immutability issue. This obj was returned from graphql call. Seems like I would have to deebug deeper. or copy said object, which I don't want to do. Commented Jul 25, 2017 at 11:43
  • 1
    You should have informed about immutability requirement. Commented Jul 25, 2017 at 11:44
  • 1
    Sorry, was not sure this is the issue. I though immutability is only valid for pure types not nested object. Guess I have some reading to do... Commented Jul 25, 2017 at 11:47

4 Answers 4

1

I hope it's not blowing up open door, but you can try reduce aproach. Immutability requirement is 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);

Sign up to request clarification or add additional context in comments.

2 Comments

Works only if there's a single element on each array. Test: jsfiddle.net/rpd9pzmt
@PedroCorso Ah, yeah, indeed. Missed the three dots after single arrays :) Fixed!
1

Not sure if I'm being helpful here, but copying the data (without t property) to a new object seems to be faster than the delete method: https://jsperf.com/removing-object-props

EDIT: I've edited the test cases to fix Kind user's code. Copying is still faster.

5 Comments

Very interesting to see that delete is so slow.
The tests are now corrected. Reduce method is actually almost as slow as deleting.
Thanks Pedro. This is useful!!
The jsperf.com link is 404.
@keithpjolley Not sure what happened. I tried to check the "My tests" page but it is giving me 404 too. It seems that I'm not the only one.
0

You can use array#forEach to delete t key.

var 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'}
                }
              ]
    }
}

Object.keys(obj.data).forEach(o => {
  obj.data[o].forEach(p => delete p['t'])
});
console.log(obj);

Comments

0

not manipulated because you're deleting output object not object from object

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'
        }
      },
    ]
  }
}

Object.keys(obj.data).forEach((item) => {
 delete obj.data[item][0].t;
});

console.log(obj.data)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.