Rodmentou
Rodmentou

Reputation: 1640

Highest performance on slicing objects on an array of objects

I want to reduce the size of an array in-memory of my NodeJS server.

I want to send only the 'header' of each object in the array, reducing the network traffic. I have this array on the server:

var lotOfThings = [
  {name: 'Watson', role: 'Mod', lotOfData: ... },
  {name: 'Sherlock', role: 'Admin', lotOfData: ...}
];

I want to remove lotOfData from all the objects within lotOfThings and send back to the user only this:

  {name: 'Watson', role: 'Mod'},
  {name: 'Sherlock', role: 'Admin'}

How can I achieve this with a good performance?

Upvotes: 0

Views: 53

Answers (2)

Ben Aubin
Ben Aubin

Reputation: 5657

Plain JS:

lotOfThings.map(function(thing) {
    return { name: thing.name, role: thing.role };
});

Upvotes: 4

pwilmot
pwilmot

Reputation: 596

using underscore.js

_.map(lotOfThings, 
    function(thing) {
        return { name: thing.name, role: thing.role };
    }
);

Upvotes: 3

Related Questions