我有一個帶有嵌套物件的 JS 陣列作為“任務串列”。它們每個都添加了不同的 Urgency 屬性。現在我的函式需要找到最高緊急值 - 將其設定為一個變數,然后將其從陣列中洗掉。
我堅持從原始陣列中洗掉它。我遇到的麻煩可能只是找到索引:
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)
uj5u.com熱心網友回復:
如果您的reduce作業正常,那么您可以使用該物件查找索引并使用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);
uj5u.com熱心網友回復:
此代碼應該可以解決您的問題
const highestUrgencyValue = Math.max(...taskList.map(t => t.stockUrgency));
const highestUrgencyIndex = taskList.findIndex(task => task.stockUrgency === highestUrgencyValue);
taskList.splice(highestUrgencyIndex, 1);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/376514.html
標籤:javascript 数组 目的 降低
