0

I have object

var o = {
   first{
      aa: 1,
      bb: 2,
      cc: 3
   },
   second: {
      aa: 2,
      dd: 1,
      ee: 10
   }
}

I need to return new object with same structure but only one property.

getProperty('second.aa');

getProperty = function(path){
   _.each(path.split('.'), function(){
      //....
   });
   // need return {second: {aa: 2}}
}

Maybe there are good solutions with underscore or angularjs? Thanx!

Sergey Kudryashov
  • 713
  • 2
  • 9
  • 30
  • [This](http://stackoverflow.com/questions/6393943/convert-javascript-string-in-dot-notation-into-an-object-reference) is a good solution – casraf Aug 13 '15 at 08:20
  • With [lodash](https://lodash.com/): http://jsfiddle.net/o8s3osbx/ – Andreas Aug 13 '15 at 08:39
  • Very nice, thanx! But I'm looking for the same solution with underscore – Sergey Kudryashov Aug 13 '15 at 08:42
  • And why not lodash? lodash is a superset of underscore and they are planning to merge them into one library. [Differences between lodash and underscore](http://stackoverflow.com/questions/13789618/differences-between-lodash-and-underscore), [The Big Kahuna: Underscore + Lodash Merge Thread](https://github.com/jashkenas/underscore/issues/2182) – Andreas Aug 13 '15 at 08:47
  • Coz I already use underscore in my project. So I don't want to use both libs.. When they will merge - it will be awesome – Sergey Kudryashov Aug 13 '15 at 08:50

0 Answers0