Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

177
Visualizações
Replace Array.prototype.includes() with a method that does not return a boolean

I have a function in Typescript in which, from a string, it returns the key of the object that contains that variable.

I'm using Array.prototype.includes() to check that the variable exists, this can return true or false, so typing the resulting variable as a string gives me a typing error.

This is the error:

Type 'unknown' cannot be assigned to type 'string'

This is my function:

let testData = {
  data1: ['CAR','PLANE'],
  data2: ['COUNTRY','CITY']
};

let asset = 'car';

function resKey(asset) {

  let res = Object.keys(testData).find(key => {
    const value = testData[key]
  
    return value.includes(asset.toUpperCase())
  })

  return res

}

console.log(resKey(asset));

I'm only going to pass it values that are in the object, so I don't need to check if it exists.

My problem: how can I modify the function so that it only returns the key without the need to check if it exists?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

As I noted in the comments you can make your current function Typescript friendly by giving it some type annotations.

Alternatively you can avoid the find() by creating a Map of the testData and just retrieving the key by value directly. Keep in mind that if there are duplicate values between different asset arrays this will return the key of the last instance (as opposed to find() which will return the key of the first instance).

let testData = {
  data1: ['CAR', 'PLANE'],
  data2: ['COUNTRY', 'CITY']
};

let asset = 'car';

function resKey(asset) {
  const resMap = new Map(Object.entries(testData).flatMap(([k, v]) => v.map(a => [a, k])));
  
  return resMap.get(asset.toUpperCase());
}

console.log(resKey(asset));

To avoid creating a new Map on every call of the function you might employ a little currying.

function resKeyFactory(data) {
  const resMap = new Map(
    Object.entries(data)
      .flatMap(([k, v]) => v.map(a => [a, k]))
  );

  return (asset) => resMap.get(asset.toUpperCase());
}

const
  testData = {
    data1: ['CAR', 'PLANE'],
    data2: ['COUNTRY', 'CITY']
  },
  asset = 'car',
  resKeyTestData = resKeyFactory(testData);

console.log(resKeyTestData(asset));
console.log(resKeyTestData('city'));

Typescript requires a high enough target to accept flatMap playground.

about 4 years ago · Juan Pablo Isaza Relatório

0

You could use Object.entries() and find().

let testData = {
  data1: ['CAR', 'PLANE'],
  data2: ['COUNTRY', 'CITY']
};

function resKey(data, asset) {
  return Object.entries(data).find(([_, v]) => {
    return v.includes(asset.toUpperCase())
  })?.[0] || "no key found";
}

console.log(resKey(testData, "car"));
console.log(resKey(testData, "something"));
console.log(resKey(testData, "city"));

If you want to type, it would look something like this.

interface IData {
  data1: string[];
  data2: string[];
}

let testData: IData = {
  data1: ['CAR', 'PLANE'],
  data2: ['COUNTRY', 'CITY']
};

function resKey(data: IData, asset: string): string {
  return Object.entries(data).find(([_, v]) => {
    return v.includes(asset.toUpperCase())
  })?.[0] || "no key found";
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda