簡體   English   中英

如何比較兩個數組項並替換,並移動到特定位置

[英]How to compare the two array items and replace,And also move to particular position

以下是包含類型、代碼和描述的員工數組的代碼。 還有另一個稱為 days 的數組,其中包含不同的時間段。

我需要將天數與employeeArr 進行比較。

如果day[0] === employeeArr[0].codeDescription ,則不要移動,如果是 !== 則它將替換 。 所以這里如果 days[1] 是每周,那么 employeeArr[4].codeDescription 移動到第一個位置。 如果 day[3] 是 Quartely,那么 employeeArr[1].codeDescription 移動到第三個位置。

 let employeeArr = [ { "empType": "da", "empCode": "1", "codeDescription": "Daily" }, { "empType": "hy", "empCode": "6", "codeDescription": "Quarterly" }, { "empType": "mo", "empCode": "4", "codeDescription": "Monthly" }, { "empType": "qu", "empCode": "5", "codeDescription": "Half Yearly" }, { "empType": "wl", "empCode": "2", "codeDescription": "Weekly" }, ] let days = ["Daily", "Weekly", "Monthly", "Quartely", "Half Yearly"];

 let employeeArr = [ { "empType": "da", "empCode": "1", "codeDescription": "Daily" }, { "empType": "hy", "empCode": "6", "codeDescription": "Quarterly" }, { "empType": "mo", "empCode": "4", "codeDescription": "Monthly" }, { "empType": "qu", "empCode": "5", "codeDescription": "Half Yearly" }, { "empType": "wl", "empCode": "2", "codeDescription": "Weekly" }, ] let days = ["Daily", "Weekly", "Monthly", "Quarterly", "Half Yearly"]; let modEmployee = [] var i, j; for (i = 0; i < days.length; i++) { for (j = 0; j < employeeArr.length; j++) { if (employeeArr[j].codeDescription === days[i]) { modEmployee.splice(i, 0, employeeArr[j]); } } } console.log(modEmployee);

輸出:

[ { empType: 'da', empCode: '1', codeDescription: 'Daily' },
  { empType: 'wl', empCode: '2', codeDescription: 'Weekly' },
  { empType: 'mo', empCode: '4', codeDescription: 'Monthly' },
  { empType: 'hy', empCode: '6', codeDescription: 'Quarterly' },
  { empType: 'qu', empCode: '5', codeDescription: 'Half Yearly' } ]

您可以創建這樣的比較函數

const comparator = (emp1, emp2) => {
    const days = ["Daily", "Weekly", "Monthly", "Quarterly", "Half Yearly"];
    return days.indexOf(emp1.codeDescription) - days.indexOf(emp2.codeDescription);
};

然后用它對employeeArr進行排序

employeeArr.sort(comparator);

您可以通過與員工一起制作天數地圖來獲得結果

let employeeMap =employeeArr.reduce((rs,el) => {let currentKey = el['codeDescription']; let currentObj = {}; currentObj[currentKey.toString()]=el;return Object.assign(rs, currentObj)}, {})
var result = days.map(el =>employeeMap[el])

console.log(result)

您可以使用 HashMap 在 O(n) 中實現這一點。

   Map<String,Integer> map = new HashMap<>();

        //Iterate employee array and store the description as key and index as value in map
        for(int i=0;i<employeeArr.length;i++)
        {
            Employee e =  employeeArr[i];
            map.put(e.description,i);
        }

        //Iterate days array, find the corresponding position in map, swao the indexing position in map and swap the values in Employee array if it is not in expected order
        for(int j=0;j<days.length;j++)
        {
            int index = map.get(days[j]);
            if(index == j)
            {
                continue;
            }
            Employee temp = employeeArr[index];
            employeeArr[index] = employeeArr[j];
            employeeArr[j] = temp;
            map.put(employeeArr[index].description, index);
            map.put(employeeArr[j].description, j);
        }


暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM