###Javascript's Map object
Javascript's Map object
You can use a Map to reduce the array of objects. A Map holds a list of key, value pairs. Each Key is unique and can be any Javascript Object or primitive type, the same for the associated value.
You could create a generic function to handle many data types. The unique index needs to be defined for the function and could be handled as a callback the takes as an argument an array item and returns a string containing the index.
Also I assume that the name is not unique and that is why you need an ID as well. I added two names each with and different ID just to test.
// test data
const data = [
{ Name: 'Joe', ID : 1 },
{ Name: 'Mary', ID : 2 },
{ Name: 'Joe', ID : 1 },
{ Name: 'Mary', ID : 2 },
{ Name: 'Biggie', ID : 3 },
{ Name: 'Joe', ID : 1 },
{ Name: 'Mark', ID : 4 }, // Added same name with different IDs
{ Name: 'Mark', ID : 5 },
];
// data the array to reduce
// indexFunc Returns a index for a given item eg (item) => item.name
// Function returns a new array contining only unique items.
function reduceArray (data, indexFunc) {
const reducedData = new Map();
data.forEach(item => reducedData.set(indexFunc(item), item) );
return [...reducedData.values()]; // convert the Map back to an array of items.
}
// Reduce the data array
const dataR = reduceArray(data, item => item.Name + item.ID);
// Show result.
dataR.forEach(item => console.log(item.Name + " : " + item.ID) );
###Javascript's Map object
You can use a Map to reduce the array of objects. A Map holds a list of key, value pairs. Each Key is unique and can be any Javascript Object or primitive type, the same for the associated value.
You could create a generic function to handle many data types. The unique index needs to be defined for the function and could be handled as a callback the takes as an argument an array item and returns a string containing the index.
Also I assume that the name is not unique and that is why you need an ID as well. I added two names each with and different ID just to test.
// test data
const data = [
{ Name: 'Joe', ID : 1 },
{ Name: 'Mary', ID : 2 },
{ Name: 'Joe', ID : 1 },
{ Name: 'Mary', ID : 2 },
{ Name: 'Biggie', ID : 3 },
{ Name: 'Joe', ID : 1 },
{ Name: 'Mark', ID : 4 }, // Added same name with different IDs
{ Name: 'Mark', ID : 5 },
];
// data the array to reduce
// indexFunc Returns a index for a given item eg (item) => item.name
// Function returns a new array contining only unique items.
function reduceArray (data, indexFunc) {
const reducedData = new Map();
data.forEach(item => reducedData.set(indexFunc(item), item) );
return [...reducedData.values()]; // convert the Map back to an array of items.
}
// Reduce the data array
const dataR = reduceArray(data, item => item.Name + item.ID);
// Show result.
dataR.forEach(item => console.log(item.Name + " : " + item.ID) );
Javascript's Map object
You can use a Map to reduce the array of objects. A Map holds a list of key, value pairs. Each Key is unique and can be any Javascript Object or primitive type, the same for the associated value.
You could create a generic function to handle many data types. The unique index needs to be defined for the function and could be handled as a callback the takes as an argument an array item and returns a string containing the index.
Also I assume that the name is not unique and that is why you need an ID as well. I added two names each with and different ID just to test.
// test data
const data = [
{ Name: 'Joe', ID : 1 },
{ Name: 'Mary', ID : 2 },
{ Name: 'Joe', ID : 1 },
{ Name: 'Mary', ID : 2 },
{ Name: 'Biggie', ID : 3 },
{ Name: 'Joe', ID : 1 },
{ Name: 'Mark', ID : 4 }, // Added same name with different IDs
{ Name: 'Mark', ID : 5 },
];
// data the array to reduce
// indexFunc Returns a index for a given item eg (item) => item.name
// Function returns a new array contining only unique items.
function reduceArray (data, indexFunc) {
const reducedData = new Map();
data.forEach(item => reducedData.set(indexFunc(item), item) );
return [...reducedData.values()]; // convert the Map back to an array of items.
}
// Reduce the data array
const dataR = reduceArray(data, item => item.Name + item.ID);
// Show result.
dataR.forEach(item => console.log(item.Name + " : " + item.ID) );
###Javascript's Map object
You can use a Map to reduce the array of objects. A Map holds a list of key, value pairs. Each Key is unique and can be any Javascript Object or primitive type, the same for the associated value.
You could create a generic function to handle many data types. The unique index needs to be defined for the function and could be handled as a callback the takes as an argument an array item and returns a string containing the index.
Also I assume that the name is not unique and that is why you need an ID as well. I added two names each with and different ID just to test.
// test data
const data = [
{ Name: 'Joe', ID : 1 },
{ Name: 'Mary', ID : 2 },
{ Name: 'Joe', ID : 1 },
{ Name: 'Mary', ID : 2 },
{ Name: 'Biggie', ID : 3 },
{ Name: 'Joe', ID : 1 },
{ Name: 'Mark', ID : 4 }, // Added same name with different IDs
{ Name: 'Mark', ID : 5 },
];
// data the array to reduce
// indexFunc Returns a index for a given item eg (item) => item.name
// Function returns a new array contining only unique items.
function reduceArray (data, indexFunc) {
const reducedData = new Map();
data.forEach(item => reducedData.set(indexFunc(item), item) );
return [...reducedData.values()]; // convert the Map back to an array of items.
}
// Reduce the data array
const dataR = reduceArray(data, item => item.Name + item.ID);
// Show result.
dataR.forEach(item => console.log(item.Name + " : " + item.ID) );