0

I have an JS array with nested objects as a "task list". They each are added with different Urgency properties. Now my function needs to find the highest urgency value - set this to a variable and then remove it from the array.

I am stuck at removing it from the original array. The trouble I have might just be finding the index:

function newTask() {

  // reducing the "task list" to just the highest urgency
  let maxObj = taskList.reduce((max, obj) => (max.stockUrgency > obj.stockUrgency) ? max : obj);
  
  outputNewtask = JSON.stringify(maxObj, null, 4); 
 
 let index = taskList.indexOf("outputNewtask")
 
 console.log(index)

halfer
  • 19,824
  • 17
  • 99
  • 186
  • How does your `taskList` looks like? – DecPK Dec 07 '21 at 08:26
  • Does this answer your question? [How can I remove a specific item from an array?](https://stackoverflow.com/questions/5767325/how-can-i-remove-a-specific-item-from-an-array) – tevemadar Dec 07 '21 at 09:20
  • May you share examples of `taskList`? Also please be aware that `outputNewtask` will be a JSON string _not an object_, and it's highly unlikely that `taskList` will contain an entry with that exact string. – evolutionxbox Dec 07 '21 at 10:08

2 Answers2

0

If your reduce works fine, then you can use that object to find the index and remove it from the list using splice:

    let maxObj = taskList.reduce((max, obj) => (max.stockUrgency > obj.stockUrgency) ? max : obj);

    taskList.splice(taskList.indexOf(maxObj), 1);

    outputNewtask = JSON.stringify(maxObj, null, 4); 
Deepak
  • 2,660
  • 2
  • 8
  • 23
0

This code should solve your problem

const highestUrgencyValue = Math.max(...taskList.map(t => t.stockUrgency));
const highestUrgencyIndex = taskList.findIndex(task => task.stockUrgency === highestUrgencyValue);
taskList.splice(highestUrgencyIndex, 1);
David
  • 734
  • 5
  • 10