Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

356
Views
How to find index nested object within array in JavaScript

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)

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

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); 
about 4 years ago · Juan Pablo Isaza Report

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);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!