typescript 如何使用JavaScript通过添加1获得没有重复值的对象数组

4xrmg8kj  于 2023-06-24  发布在  TypeScript
关注(0)|答案(1)|浏览(163)

我有对象arrlist和参数uid的数组
如果属性value有重复的值(忽略null),并且iduid不相同,则自动递增该值,直到没有重复的值。
如何使用JavaScript实现上述场景。试过了但卡住了

var uid1 = 26
var arrlist1 = [
  {id: 11 , value: 2},
  {id: 14, value: null},
  {id: 28, value: 1},
  {id: 26, value: 3},
  {id: 78, value: 3}
]

Expected Output

[
  {id: 11 , value: 2},
  {id: 14, value: null},
  {id: 28, value: 1},
  {id: 26, value: 3},
  {id: 78, value: 4}
]

In the above scenario, 
{id: 26, value: 3} is not incremented since id matches with uid

for `id` 78, `value` 3 is duplicate, so increment it
 {id: 78, value: 4}
and no more duplicate return expected output.
var uid2 = 78
var arrlist2 = [
  {id: 11 , value: 2},
  {id: 14, value: null},
  {id: 28, value: 1},
  {id: 26, value: 3},
  {id: 78, value: 1}
]

Expected Output

[
  {id: 11 , value: 3},
  {id: 14, value: null},
  {id: 28, value: 2},
  {id: 26, value: 4},
  {id: 78, value: 1}
]

In the above scenario, 
{id: 78, value: 1} is not incremented since id matches with uid

for `id` 28, `value` 1 is duplicate, so increment it becomes
{id: 28, value: 2}
for `id` 11 `value` 2 is duplicate, so increment it becomes
{id: 11, value: 3}
for `id` 26 `value` 3 is duplicate, so increment it becomes
{id: 26, value: 4}

and no more duplicate return expected output.
Tried
const result = this.getArrObj(arrlist1, uid1)

function getArrObj (arrlist, uid) {
  const totalobj = arrlist.filter(e=>e!==null).length);

  var finalArrObj = arrlist.map(itm => ({
    ...itm,
    value: (itm.value && (itm.id !== uid && itm.value <= totalobj)) ?  
      (itm.value || 0) + 1 : itm.value
  })

}
pgx2nnw8

pgx2nnw81#

function incrementDuplicate(arrlist2, id) {
  // Helper function to check for duplicates
  function hasDuplicates(array) {
    return new Set(array).size !== array.length;
  }

  // Get the values from the array
  const values = arrlist2.map(obj => obj.value);

  // Check for duplicate values (other than the passed id)
  if (hasDuplicates(values) && id !== null) {
    const duplicateValue = values.find(value => value !== null && values.indexOf(value) !== values.lastIndexOf(value));

    if (duplicateValue !== undefined) {
      // Find the object with the duplicate value
      const duplicateObject = arrlist2.find(obj => obj.value === duplicateValue && obj.id !== id);

      // Increment the value of the duplicate object
      duplicateObject.value++;

      // Keep track of modified id
      const modifiedId = duplicateObject.id;

      // Recursively check for additional duplicates
      incrementDuplicate(arrlist2, modifiedId);
    }
  }

  return arrlist2;
}

解决方案2

function incrementDuplicate(arrlist2, id) {
  const valueToIdMap = new Map();
  for (const obj of arrlist2) {
     if (obj.id !== id) {
      valueToIdMap.set(obj.value, obj.id);
     }
  }
  const matchingObj = arrlist2.find(o => o.id === id);
  const valueToSearch = matchingObj.value;
  for (const [k,v] of valueToIdMap) {
      if (k >= valueToSearch) {
          const indexToModify = arrlist2.findIndex(o => o.id === v);
          arrlist2.splice(indexToModify, 1, { id: v, value: k + 1});
      }
  }
  return arrlist2;
}

相关问题