Search code examples
javascriptobjectlodash

Update object from objectlist with next object key


Using Javascipt and have lodash installed in my project.

My input is as follows:

const fields = {
  accountBalance: {
    id: 1,
    field: 'input',
  },
  totalTrades: {
    id: 2,
    field: 'input',
  },
  winRate: {
    id: 3,
    field: 'slider'
  },
  reward: {
    id: 4,
    field: 'input',
  },
  risk: {
    id: 5,
    field: 'slider',
  }
}

My output I would like to have:

const fields = {
  accountBalance: {
    id: 1,
    field: 'input',
    nextFieldFocus: 'totalTrades'
  },
  totalTrades: {
    id: 2,
    field: 'input',
    nextFieldFocus: 'reward'
  },
  winRate: {
    id: 3,
    field: 'slider'
  },
  reward: {
    id: 4,
    field: 'input',
    nextFieldFocus: 'accountBalance'
  },
  risk: {
    id: 5,
    field: 'slider',
  }
}

Each object the field value can be either 'input' or a 'slider'. Each time it update the field value I call a function that update the fields (the expected output above). Each field where a field is a input, update the nextFieldFocus with the next key of the objectlist where the field is a input. If it can't find it in the remaining order, start over at the start of the object list where the field is a input.

What I tried:

const fields = {
  accountBalance: {
    id: 1,
    field: 'input',
  },
  totalTrades: {
    id: 2,
    field: 'input',
  },
  winRate: {
    id: 3,
    field: 'slider'
  },
  reward: {
    id: 4,
    field: 'input',
  },
  risk: {
    id: 5,
    field: 'slider',
  }
}

const keys = Object.keys(fields);

for (let i = 0; i < keys.length; i++) {
  const currentKey = keys[i];
  if (fields[currentKey].field === 'input') {
    let nextKey = keys[i + 1];
    while (nextKey && fields[nextKey].field !== 'input') {
      nextKey = keys[++i % keys.length];
    }
    fields[currentKey].nextFieldFocus = nextKey;
  }
}

console.log(fields);

enter image description here

Problem I am facing is that if it can't find the next object where field is a input that the nextFieldFocus won't be filled. Ideal is that it start checking from the start but I don't know how.


Solution

  • const fields = {
      accountBalance: {
        id: 1,
        field: 'input',
      },
      totalTrades: {
        id: 2,
        field: 'input',
      },
      winRate: {
        id: 3,
        field: 'slider'
      },
      reward: {
        id: 4,
        field: 'input',
      },
      risk: {
        id: 5,
        field: 'slider',
      }
    }
    
    const keys = Object.keys(fields);
    
    for (let i = 0; i < keys.length; i++) {
      const currentKey = keys[i];
      const currentObj = fields[currentKey]
      if (currentObj.field === 'input') {
        for (let j = 0; j < keys.length; j += 1) {
          const nextKey = keys[(i + j + 1) % keys.length];
          const nextObj = fields[nextKey]
          if (nextObj.field !== 'input') continue
          currentObj.nextFieldFocus = nextKey;
          break
        }
      }
    }
    
    console.log(fields);