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
andMarketingCode
value isstring/number/Boolean
.
because I have tried some of the below answers and i am getting this below error.
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 );
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}]))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With