I'm getting an array and I needed to find a duplicate ID inside:

var aDuplicateId = [];
            //get the duplicated configuration indexs
            for (var i = 0; i < aConfigurations.length; i++) {
                //in case the index is the duplicate array skip
                if (aDuplicateId.indexOf(i) >= 0) {
                var iId = aConfigurations[i]._metadata.id;
                //find duplicate ids
                var iIndex = _.findLastIndex(aConfigurations, function(o) {
                    return o._metadata.id === iId;
                //add the duplicate id index to array
                if (iIndex >= 0 && iIndex !== i) {

1 Answer 1


So essentially, you're just getting the unique configuration IDs. This can easily be done by converting your aConfigurations into an array of IDs and using Set to weed out duplicates. Then you can convert the set instance back into an array using Array.from.

const configIds = aConfigurations.map(config => config._metadata.id);
const configIdsSet = new Set(configIds);
const configIdsUnique = Array.from(configIdsSet);

Alternatively, without ES6, you can just use array.reduce to collect IDs as keys of an object and then use Object.keys to extract the keys into an array.

var configMap = aConfigurations.reduce(function(configs, config){
  configs[config._metadata.id] = true;
  return configs;
}, {});

var configIdsUnique = Object.keys(configMap);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.