Home > front end >  Cannot use .some() with array of objects
Cannot use .some() with array of objects

Time:10-27

I'm trying to check wether my array of objects contains an atribute with a specified value. For this I thought of using array.some(), instead of a foreach loop. However I keep getting an error:

error TS2345: Argument of type '(element: ListaDetalhePendenciaAprovacao) => void' is not assignable to parameter of type '(value: ListaDetalhePendenciaAprovacao, index: number, array: ListaDetalhePendenciaAprovacao[]) => boolean'. Type 'void' is not assignable to type 'boolean'.

The code is as follows:

let existeApr = this.pendenciasList.some(element =>{
      !isUndefined(element.isSelected) && element.isSelected
    })

   let existeRej = this.pendenciasList.some(element =>{
      !isUndefined(element.isSelected) && element.isSelected
    })

    existeApr ? this.permiteJustificarAprov=true : this.permiteJustificarAprov=false;
    existeRej ? this.permiteJustificarRej=true : this.permiteJustificarRej=false;

where:

public pendenciasList: ListaDetalhePendenciaAprovacao[] = [];

And the model class ListaDetalhePendenciaAprovacao.model.ts is:

export class ListaDetalhePendenciaAprovacao {
  public codigoPendencia: number;
  public descricaoCedente: string;
  public descricaoSacado: string;
  public codigoTitulo: number;
  public dataVencimento: Date;
  public valorTitulo: number;
  public valorCorrecao: number;
  public valorCorrigido: number;
  public codigoAcao: number;
  public acaoTomada: string;
  public complementoAcao: string;
  public dataRegistroAcao?: Date;
  public acaoTomadaPor: string;
  public numeroTitulo: string;
  public atrasoPrioritario: boolean;
  public codigoOrigem: number;
  public idPendenciaTitulo?: number;
  public isSelected?: boolean
}

I've also been hving the same issue with array.findIndex(). I apologize for the atributes being in my native language, I don't think it should be an issue.

Thanks in advance!

CodePudding user response:

This is happening because of the curly braces in the callback that you pass to some. If you open the curly braces, then you need to return a boolean value from it:

let existeApr = this.pendenciasList.some(element =>{
  return !isUndefined(element.isSelected) && element.isSelected
})

But for a one liner like yours, you should drop the curly braces altogether and go with something like this:

let existeApr = this.pendenciasList.some(element =>
  !isUndefined(element.isSelected) && element.isSelected)
  • Related