2

Why documentation on page:

Lo-Dash documentation

Says:

_.pick(object, [callback], [thisArg])

Creates a shallow clone of object composed of the specified properties. Property names may be specified as individual arguments or as arrays of property names. If a callback is provided it will be executed for each property of object picking the properties the callback returns truey for. The callback is bound to thisArg and invoked with three arguments; (value, key, object).

When I test it I get deep copy. There is no conection betwen orginal object and object created with this method.

Sysrq147
  • 1,359
  • 4
  • 27
  • 49
  • Just tried it and it's shallow... http://jsfiddle.net/4BHn6/ – Anthony Chu Jun 28 '14 at 21:04
  • Sorry it is some problem on my side. In Fiddle it works like shallow copy, in my enviroment like deep. I am confused :/ – Sysrq147 Jun 28 '14 at 22:07
  • 1
    So your nested objects or arrays will be passed by reference. That is, they will appear in your copy to make it seem like a deep copy but they only reference the originals and are not an actual copy thus being a shallow copy. – Jeff Adams Jan 09 '15 at 16:25

1 Answers1

1
var obj = {x: 5},
    foo = {k1: obj, k2: {a: 0}},
    picked = _.pick(foo, 'k1');  // -> picked = { k1: {x: 5 } }

picked.k1.x = 6;
console.log(picked);             // { k1: { x: 6 } }
console.log(obj);                // { x: 6 }
chepukha
  • 2,371
  • 3
  • 28
  • 40