Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get duplicated values from an array by checking with two element

If My array have this below data

let array = [{ name: "Ramesh", SalseVersion: 10, MarketingCode: 11 },
        { name: "Suresh", SalseVersion: 12, MarketingCode: 13 },
        { name: "Siva", SalseVersion: 10, MarketingCode: 14 },
        { name: "Sakthi", SalseVersion: 10, MarketingCode: 11 },...]

Then I am expecting this below result

[{ name: "Ramesh", SalseVersion: 10, MarketingCode: 11 },
 { name: "Sakthi", SalseVersion: 10, MarketingCode: 11 }]

If My array have this below data

let array = [{ name: "Ramesh", SalseVersion: 10, MarketingCode: 11 },
        { name: "Suresh", SalseVersion: 12, MarketingCode: 14},
        { name: "Siva", SalseVersion: 12, MarketingCode: 14 },
        { name: "Sakthi", SalseVersion: 10, MarketingCode: 11 },...]

Then I am expecting this below result.

        [{ name: "Ramesh", SalseVersion: 10, MarketingCode: 11 },
        { name: "Sakthi", SalseVersion: 10, MarketingCode: 11 }
        { name: "Suresh", SalseVersion: 12, MarketingCode: 14},
        { name: "Siva", SalseVersion: 12, MarketingCode: 14 }]

I have tried this way : Get all non-unique values (i.e.: duplicate/more than one occurrence) in an array

let arr = [{ name: "Ramesh", SalseVersion: 10, MarketingCode: 11 },
        { name: "Suresh", SalseVersion: 12, MarketingCode: 13 },
        { name: "Siva", SalseVersion: 10, MarketingCode: 14 },
        { name: "Sakthi", SalseVersion: 10, MarketingCode: 11 }]


var sorted_arr = arr.slice().sort();
var results = [];
for (var i = 0; i < sorted_arr.length - 1; i++) {
    if (sorted_arr[i + 1].SalesVersion == sorted_arr[i].SalesVersion && sorted_arr[i + 1].MarketingCode == sorted_arr[i].MarketingCode) {
        results.push(sorted_arr[i]);
    }
}

console.log(results);

But unable to get the duplicated values. Could you please resolve this?

Note: Please mind it should be work as well if the SalseVersion and MarketingCode value is string/number/Boolean.

because I have tried some of the below answers and i am getting this below error.

enter image description here

like image 769
Ramesh Rajendran Avatar asked Jan 03 '23 09:01

Ramesh Rajendran


2 Answers

You can use reduce

The first reduce will group the values using the concatenated values of SalseVersion and MarketingCode.

The second reduce will check if the group has more than 1 element. If there is concat the values into 1 array.

let array = [{ name: "Ramesh", SalseVersion: 10, MarketingCode: 11 },{ name: "Suresh", SalseVersion: 12, MarketingCode: 14},{ name: "Siva", SalseVersion: 12, MarketingCode: 14 },{ name: "Sakthi", SalseVersion: 10, MarketingCode: 11 }];

let result = Object.values(array.reduce((c, v) => {
  let k = v.SalseVersion + '-' + v.MarketingCode;
  c[k] = c[k] || [];
  c[k].push(v);
  return c;
}, {})).reduce((c, v) => v.length > 1 ? c.concat(v) : c, []);

console.log( result );
like image 156
Eddie Avatar answered Jan 13 '23 12:01

Eddie


It looks like you're trying to find elements for which there exists at least one other element in the array with similar properties. You can use .filter for this, followed by sort:

const dupesFromArr = array => {
  const filteredArr = array.filter(({ name, SalseVersion, MarketingCode }, i, arr) => (
    arr.find((item, findI) => (
      findI !== i &&
      SalseVersion === item.SalseVersion &&
      MarketingCode === item.MarketingCode
    ))
  ));
  return filteredArr.sort((a, b) =>
    String(a.SalseVersion).localeCompare(String(b.SalseVersion), 'kn') ||
    String(a.MarketingCode).localeCompare(String(b.MarketingCode), 'kn')
  );
};

console.log(dupesFromArr([{name:"Ramesh",SalseVersion:10,MarketingCode:11},{name:"Suresh",SalseVersion:12,MarketingCode:13},{name:"Siva",SalseVersion:10,MarketingCode:14},{name:"Sakthi",SalseVersion:10,MarketingCode:11}]))
console.log(dupesFromArr([{name:"Ramesh",SalseVersion:10,MarketingCode:11},{name:"Suresh",SalseVersion:12,MarketingCode:14},{name:"Siva",SalseVersion:12,MarketingCode:14},{name:"Sakthi",SalseVersion:10,MarketingCode:11}]))
like image 40
CertainPerformance Avatar answered Jan 13 '23 13:01

CertainPerformance