Home > other >  Update object from objectlist with next object key
Update object from objectlist with next object key

Time:01-07

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.

CodePudding user response:

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);

CodePudding user response:

You can do this with a single iteration through the object. Just save the first "input" and the previous input object to 2 variables. Whenever an "input" field is reached, update the previous "input" with the current key. When the last key is being iterated, update the previous input with the first input key.

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"}};

let first, 
    previous,
    entries = Object.entries(fields);

entries.forEach(([key, current], i) => {
  if (current.field == 'input') {
    first ??= key

    if (previous) previous.nextFieldFocus = key
    previous = current
  }

  // last key in the object
  if (i === entries.length-1)
    previous.nextFieldFocus = first
})

console.log(fields)

CodePudding user response:

const keys = Object.keys(fields);

keys.forEach((field, idx) => {
  if (fields[field].field !== 'input') return;

  fields[field].nextFieldFocus = keys[idx   1];
}) 
  • Related