-2

I know variations of this question have been asked but bear with me.

I have the following array containing objects (which are routines):

const PREMADE_ROUTINES = [
  {
    itemIds: ['makebed', 'drinkwater', 'quickstretch', 'hotshower',  'brushteeth', 'smallsnack', 'getdressed', 'todolist', 'declutterinbox',],
    routineDuration: DEFAULT_ROUTINE_ITEMS.getItemsFromIds(PASS THIS OBJECTS itemIds HERE)
  }
]

How could I access the itemIds in this case within each of the objects in the PREMADE_ROUTINES array?

In this case, I would need to pass the objects itemIds as an argument to the function. I could do this if the object wasn't in an array with a get(). However, I don't know how to do it in this case. Any ideas?

Ideally, I would like to simply access the routineDuration key by looping and simply accessing it.

Walter Monecke
  • 2,386
  • 1
  • 19
  • 52
  • You might want to give each `itemId` its own function: `{ itemId: 'makebed', routineDuration: DEFAULT_DURATION } ` ... etc, as this might be more ergonomic to work with. – rath Oct 22 '19 at 14:18
  • Make `routineDuration` a [getter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get). Possible duplicate of [Self-references in object literals / initializers](https://stackoverflow.com/questions/4616202/self-references-in-object-literals-initializers) – Tyler Roper Oct 22 '19 at 14:19
  • @Rajesh but I would like to access it without having to loop. I would like the key to calc its value on the go without having to do anything else. – Walter Monecke Oct 22 '19 at 14:21
  • Try `routineDuration: DEFAULT_ROUTINE_ITEMS.getItemsFromIds(PREMADE_ROUTINES[0].itemIds)`. You may have to use `var` instead of `const` – Rajesh Oct 22 '19 at 14:23
  • @TylerRoper yeah and how do you access the object?? it is not the same question... I can't do `foo.getItemIds` because foo is within an array! – Walter Monecke Oct 22 '19 at 14:24
  • @WalterMonecke I suppose I'm confused, as you yourself said *"Ideally, I would like to simply access the routineDuration key **by looping and simply accessing it**"*. So... loop through `PREMADE_ROUTINES` as you would any other array, and for each item, access the `routineDuration` property. – Tyler Roper Oct 22 '19 at 14:28
  • @TylerRoper yes, but I was meaning when rendering the items say in a component. My question is specifically if this can be done by referencing the object itself - like with a `get()`. I clearly show what my question is about in the code. – Walter Monecke Oct 22 '19 at 14:30
  • It's about as clear as mud. `get routineDuration() { return DEFAULT_ROUTINE_ITEMS.getItemsFromIds(this.itemIds); }` ... `PREMADE_ROUTINES.forEach(i => console.log(i.routineDuration));` - Is [this](https://jsfiddle.net/69Lkaqdr/) not what you're after? – Tyler Roper Oct 22 '19 at 14:38

2 Answers2

0

In your particular context the best solution is to break away the itemIds declaration. This is so you don't run into this issues when populating it later on.

    const DEFAULT_ROUTINES = [
        ...
    ]

    const PREMADE_ROUTINES = [ {
    itemIds: DEFAULT_ROUTINES,
    routineDuration: yourFunctionHere(DEFAULT_ROUTINES)

]}

I notice your data structure is a bit complex. It might be worth refactoring it, and introducing a couple utility / filtering methods to make your life easier in the future.

rath
  • 3,655
  • 1
  • 40
  • 53
0

You could create a method returning your routines object. That way it'd be more reusable.

const createRoutine = (routines) => ({
    itemIds: routines,
    routineDurations: getRoutineDurations(routines)
});

const PREMADE_ROUTINES = [
    createRoutine(['makebed', 'drinkwater', 'quickstretch']),
    createRoutine(['hotshower',  'brushteeth', 'smallsnack']),
];
Grabofus
  • 1,924
  • 14
  • 17