1

I have an array of strings like

let arr = ["id", "photos[0]"]

And source object like

let obj = {
  id: 111,
  name: "nick",
  photos: [{photo1: tbd},{photo2: tbd}]
}

And i need to filter this obj by given strings to get new obj

{id: 111, photos: {photo1: tbd}}

any elegant solutions?

georgeawg
  • 48,608
  • 13
  • 72
  • 95
scrudge
  • 11
  • 1
  • https://stackoverflow.com/questions/38750705/filter-object-properties-by-key-in-es6 <-- I believe this does what you want. Filter an object by a set of allowed properties. – aet Mar 05 '20 at 19:37
  • Is the goal to have the string `"photos[0]"` act like code, e.g., it'll return the first element of the data's `photos` array? – Dave Newton Mar 05 '20 at 19:38
  • @DaveNewton How is that not covered by the answers? One of the examples is `'part3[0].name'`... – Heretic Monkey Mar 05 '20 at 19:38
  • @HereticMonkey Ah, yes. It is covered by the answers, although I think the question title should be edited to better reflect the content. – Dave Newton Mar 05 '20 at 19:40
  • See [AngularJS has `$scope.$eval`](https://stackoverflow.com/a/60553207/5535245) for a way to do it with AngularJS. – georgeawg Mar 05 '20 at 20:25

0 Answers0