7

What is the most efficient way in JavaScript to clone an array of uniform objects into one with a subset of properties for each object?

UPDATE

Would this be the most efficient way to do it or is there a better way? -

var source = [
 {
 id: 1,
 name: 'one',
 value: 12.34
 },
 {
 id: 2,
 name: 'two',
 value: 17.05
 }
];
// copy just 'id' and 'name', ignore 'value':
var dest = source.map(function (obj) {
 return {
 id: obj.id,
 name: obj.name
 };
});
asked Jun 5, 2015 at 11:06
4
  • possible duplicate of How to get a subset of a javascript object's properties Commented Jun 5, 2015 at 11:09
  • 1
    @artm, that question is about one object, while I'm asking about an array of objects. Commented Jun 5, 2015 at 11:13
  • @Khalid, that question doesn't cover sub-set of properties. Commented Jun 5, 2015 at 11:13
  • Well, you could use stackoverflow.com/a/17781590/3309109 for example and call it for each object in your array. Commented Jun 5, 2015 at 11:16

3 Answers 3

10

using Object Destructuring and Property Shorthand

let array = [{ a: 5, b: 6, c: 7 }, { a: 8, b: 9, c: 10 }];
let cloned = array.map(({ a, c }) => ({ a, c }));
console.log(cloned); // [{ a: 5, c: 7 }, { a: 8, c: 10 }]
answered Sep 5, 2016 at 15:24
2

First define a function that clone an object and return a subset of properties,

Object.prototype.pick = function (props) {
 return props.reduce((function (obj, property) {
 obj[property] = this[property];
 return obj;
 }).bind(this), {});
}

Then define a function that clone an array and return the subsets of each object

function cloneArray (array, props) { 
 return array.map(function (obj) { 
 return obj.pick(props);
 });
}

Now let's say you have this array :

var array = [
 { name : 'khalid', city : 'ifrane', age : 99 },
 { name : 'Ahmed', city : 'Meknes', age : 30 }
];

you need to call the function and pass the array of properties you need to get as result

cloneArray(array, ['name', 'city']);

The result will be :

[
 { name : 'khalid', city : 'ifrane' },
 { name : 'Ahmed', city : 'Meknes' }
]
answered Jun 5, 2015 at 11:19
1
  • 1
    That's even more generic an answer than I was looking for, i.e. I didn't mind specifying columns during the copy operation. Your answer is more reusable and clean. Thank you! Commented Jun 5, 2015 at 11:26
1

Performance-wise, that would be the most efficient way to do it, yes.

answered Jun 5, 2015 at 11:25
1
  • Thank you! I did an update after the first answer, which gives a very good and more generic approach. Commented Jun 5, 2015 at 11:29

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.