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

274
Visualizações
How to show an object inside of another object as a normal text or a string?
A = {
region1: {
city1: [option1, option2, option3],
city2: [option1, option2, option3]
},
region2: {
city1: [option1, option2, option3],
city2: [option1, option2, option3]
}

How can I make city1 in region2 show up in console.log as a normal text or string ?

For example if I right A["region2"]["city1"][1] in console.log the result will be option2 and it will show up as normal text. But if I write A["region2"]["city1"] the result will show all of the array inside of city1

I need city1 to show up in console.log plain without the array inside of it nor anything else (just like a normal text or string)

I mean the object city1 not the content of it just the object it self.

I need the result in console.log to be just like that (city1)

(not the content of city1)

about 4 years ago · Santiago Gelvez
3 Respostas
Responde à pergunta

0

This is an object-oriented approach involving the override of the toString() method

function Area(name, regionList) {
    this.name = name;
    this.regionList = regionList;
}

function Region(name, cityList) {
    this.name = name;
    this.cityList = cityList;
}

function City(name, optionList) {
    this.name = name;
    this.optionList = optionList;
}

Area.prototype.toString = function () {
    return this.name;
};

Region.prototype.toString = function () {
    return this.name;
};

City.prototype.toString = function () {
    return this.name;
};

var city = new City('Lugano', ['lago', 'montagna']);

var region = new Region('Ticino', [city]);

var area = new Area('Svizzera', [region]);

console.log(area.toString());
console.log(area.regionList[0].toString());
console.log(area.regionList[0].cityList[0].toString());
about 4 years ago · Santiago Gelvez Relatório

0

You can get the object keys using the Object.keys(). Passing the A.region1 inside the function will return you the array ['city1', 'city2'] and then you can get the key that you want by its index.

const obj = {
  'region1': {
    'city1': ['option1', 'option2', 'option3'],
    'city2': ['option1', 'option2', 'option3']
  },
  'region2': {
    'city1': ['option1', 'option2', 'option3'],
    'city2': ['option1', 'option2', 'option3']
  }
}

console.log(Object.keys(obj.region1)[0]);

about 4 years ago · Santiago Gelvez Relatório

0

Join is there to rescue

console.log(A["region2"]["city1"].join(" "))

You can specify comma, space or anything you wish to split the elements inside join.

UPDATE

As the requirement is to display the key in console, you can traverse the keys of objects using Object.keys, and so I use it to find the index of the key with value "city1" and then use that index to print it. This way, you can specify any desired key name.

console.log(Object.keys(A["region2"])[Object.keys(A["region2"]).indexOf("city1")])
about 4 years ago · Santiago Gelvez 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