Find array and print error if it has repeated with find

0

I need to find the value of an array and delete it. I have been told that I can do it with .find (). Actually I have not been able to create the correct code in typescript. I have all the values of the array within a for and within that same for, I must find the duplicate array values and take error.

repeatCostCenter() {
for (let found = 0; found < this.costCenterOptions.length; found++) {
  let element = this.costCenterOptions[found];
  this.costCenterOptions.find()
}

I think of ... If I find that the value of the array [1] is equal to the value of the array [0], print me a message saying that I have two equal values.

I have 5 Array. Each array, has 1 object. Each object has an id. I want to find that id. I want to find the id of an object, after finding that id, look for if that id is duplicated or repeated inside another array.

The id is within the following

 setCostCenters(costCenters: GetCostCenterRS[]) {
this.costCenters = costCenters;
this.costCenterOptions = this.costCenters.map(
    costCenter => {
        const option: SelectItem = {
            label: costCenter.CostCenterName,
            value: costCenter.id,
        };
        return option;
    }
);

}

The id would be costCenterOption [array] .value.

How do I find that id and see if it is repeated with other arrays?

    
asked by Jhonatan Cardona Valencia 23.05.2018 в 15:11
source

1 answer

0

Starting from " The id would be costCenterOption[array].value ", because the rest is quite confusing, and considering that you want to see if that value is repeated in another position of costCenterOption you could do the following:

Clarifications:

  

As an example I did it with a list of ID directly, but as   you have an object within the array that has the ID, you must do    currentValue.value for comparison and console.log , the rest of the logic follows   the same.

var costCenterOption = [2,10,4,2,6,10,5,8,4,3,8];
this.costCenterOption.sort();
this.costCenterOption.forEach(function (currentValue, index, array) {
    if(index !== array.length && currentValue === array[index+1]){
            console.log(currentValue  + ' es un ID repetido');
    }
});

Explanation:

  

First you order the ID list, the repeated ones being numeric   will be next to each other, then you should only see what ID has in the   next position your equal.

    
answered by 23.05.2018 в 16:20