extjs JS前端-查找重复项的计数,并在JavaScript数组列表中为多次出现追加值(C#)

tgabmvqs  于 2022-11-04  发布在  Java
关注(0)|答案(2)|浏览(143)

我有一个数组列表,每个数组中有多个属性。请参考下面的示例:

FinalArray=[
{ColumnName:'Name',ID:6,TranslatedText:'Name',Category:'Assigned Employee'},
{ColumnName:'Name',ID:7,TranslatedText:'Name',Category:'Requested Employee'},
{ColumnName:'Designation',ID:8,TranslatedText:'Designation',Category:'Employee'},
{ColumnName:'Occupation',ID:9,TranslatedText:'Occupation',Category:'Assigned Employee'},
{ColumnName:'Occupation',ID:10,TranslatedText:'Occupation',Category:'Requested Employee'}
];

我的任务是找出连续重复的元素(指数组列表中的translated text属性),并为所有出现的元素追加“rep -”。因此,在app中,列表将如下所示:
| 数据行名称|类别|
| - -|- -|
| 销售代表-名称|分配的员工|
| 销售代表-名称|请求的员工|
| 名称|员工|
| 销售代表-职业|分配的员工|
| 销售代表-职业|请求的员工|
我在比较元素和追加值方面遇到了麻烦。

beq87vna

beq87vna1#

您可以使用Map,并将对象循环两次来完成此作业:

FinalArray=[
{ColumnName:'Name',ID:6,TranslatedText:'Name',Category:'Assigned Employee'},
{ColumnName:'Name',ID:7,TranslatedText:'Name',Category:'Requested Employee'},
{ColumnName:'Designation',ID:8,TranslatedText:'Designation',Category:'Employee'},
{ColumnName:'Occupation',ID:9,TranslatedText:'Occupation',Category:'Assigned Employee'},
{ColumnName:'Occupation',ID:10,TranslatedText:'Occupation',Category:'Requested Employee'}
];

let map = new Map();
FinalArray.forEach((obj)=>
{
  let count = map.get(obj.TranslatedText);
  if (count)
  {
    map.set(obj.TranslatedText, count + 1);
  }
  else
  {
    map.set(obj.TranslatedText, 1);
  }
});
FinalArray.forEach((obj)=>
{
  let count = map.get(obj.TranslatedText);
  if (count > 1)
  {
    obj.TranslatedText = "Rep - " + obj.TranslatedText;
  }

});
console.log(FinalArray);

但是,这会将Rep -添加到所有重复项的前面。如果您只需要连续的重复项,这会很难看,但它是有效的:

FinalArray=[
{ColumnName:'Name',ID:6,TranslatedText:'Name',Category:'Assigned Employee'},
{ColumnName:'Name',ID:7,TranslatedText:'Name',Category:'Requested Employee'},
{ColumnName:'Designation',ID:8,TranslatedText:'Designation',Category:'Employee'},
{ColumnName:'Occupation',ID:9,TranslatedText:'Occupation',Category:'Assigned Employee'},
{ColumnName:'Occupation',ID:10,TranslatedText:'Occupation',Category:'Requested Employee'}
];

let prevObj, foundOne;
FinalArray.push({}); // Removed later via pop(); it causes
                     // one additional iteration so that
                     // the orignial last-object gets processed
FinalArray.forEach((obj)=>
{
  if (prevObj && prevObj.TranslatedText === obj.TranslatedText)
  {
    foundOne = true;
    prevObj.TranslatedText = "Rep - " + prevObj.TranslatedText;
  }
  else
  {
    if (foundOne)
    {
      prevObj.TranslatedText = "Rep - " + prevObj.TranslatedText;
      foundOne = false;
    }
  }
  prevObj = obj;
});
FinalArray.pop();

console.log(FinalArray);
kuarbcqp

kuarbcqp2#

下面是一种不同的方法,它在数组上使用map函数:

let FinalArray=[
{ColumnName:'Name',ID:6,TranslatedText:'Name',Category:'Assigned Employee'},
{ColumnName:'Name',ID:7,TranslatedText:'Name',Category:'Requested Employee'},
{ColumnName:'Designation',ID:8,TranslatedText:'Designation',Category:'Employee'},
{ColumnName:'Occupation',ID:9,TranslatedText:'Occupation',Category:'Assigned Employee'},
{ColumnName:'Occupation',ID:10,TranslatedText:'Occupation',Category:'Requested Employee'}
];

const key = 'ColumnName', prefix = 'Rep - ';

FinalArray.map((column, index, array) => {
        if(index > 0 && array[index-1][key] === column[key]) {
      array[index-1][key] = prefix + column[key];
      array[index][key] = prefix + column[key];
    }

})

console.table(FinalArray);

相关问题