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

182
Visualizações
Finding objects within objects

I'm trying to search inside an object array to see if any object contains the object I'm looking for and assign it to a variable.

This is the interface that I'm using. Basically I have an array of countries, each of which has it's own array of cities.

import { ICity } from "./city";

export interface ICountry {
    name: string,
    capital: string,
    language: string,
    population: number,
    density: number,
    area: number,
    majorCities: ICity[]
}

The object I'm looking for is the city parameter of this function, but it always returns undefined. What is the best way to find country which a certain city belongs to?

remove(city: ICity): void {
    var country;
    this.countries.forEach(cn => {
      if (cn.majorCities.includes(city)) {
        country = cn;
        console.log(cn);
      }
    });
    console.log(country);
  }
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You should do like below

const countries = [{
    name: 'Iran',
    cities: ['Shiraz', 'Tehran']
  },
  {
    name: 'Germay',
    cities: ['Berlin']
  }
]

const findCity = (city) => {
  countries.forEach(country => {
    if (country.cities.includes(city))
      console.log(city, 'has founded!')

  })
}
findCity('Shiraz')

about 4 years ago · Juan Pablo Isaza Relatório

0

The best way (in my opinion) is to store country id in city so you can find it easier.

But in this case you can do it like below:

remove(city: ICity): void {
  var country;
  this.countries.forEach((cn) => {
    if (cn.majorCities.find(c => c.toLowerCase() === city.toLowerCase())) {
      country = cn;
      console.log(cn);
    }
  });
  console.log(country);
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Your ICity type object is not just a simple string I assume, so a check like:

if (cn.majorCities.includes(city)) 

would just return true if one of majorCities elements is the actual instance referenced via the city variable.

As your ICity interface surely consists of something like a name property e.g.

interface ICity {
name: string
}

you should check for such a string-type property.

if (cn.majorCities.some((el) => {
        return el.name == city.name

    })) {
    // do something
}
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