M
M
MrDrap2020-08-20 17:57:53
JavaScript
MrDrap, 2020-08-20 17:57:53

How to do recursion correctly?

There is an object

let entityTree = {
  account: ['account_subscriptions', 'device'],
  device: 'device_subscriptions'
}

If an account comes in, then it should be an array ['account', 'account_subscriptions', 'device', 'device_subscriptions'].

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2020-08-20
@MrDrap

function xxx(key) {
  let val = entityTree[key];
  const nextKey = Array.isArray(val) && (val = [...val], val.pop());
  return val ? [].concat(key, val, xxx(nextKey)) : [];
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question