Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

218
Vistas
How do I loop through Javascript object checking key and values?

I have a Javascript object :

const mapping = {

  'Mind Management': {
    type: 'average',
    linked_topics: ['Digital Detox', 'Small Scaling', 'Simplexity'],
    edge: 'Zeroed Out'
  },

  'Ancient Wisdom': {
    type: 'direct',
    edge: 'Roots Revival'
  }

};

I want to iterate through this object and check if the key or the linked_topics (if present) of the object matches a string value.

const stringToBeMatched = 'Simplexity'

Code that I tried:

for (var key in mapping) {
  if(key === stringToBeMatched || mapping[key].linked_topics.includes(stringToBeMatched) ) {
    console.log(`found ${stringToBeMatched} in ${key}`);
  }
}

I'm getting the following eslint error with this:

ESLint: for..in loops iterate over the entire prototype chain, which is virtually never what you want. Use Object.{keys,values,entries}, and iterate over the resulting array.(no-restricted-syntax)

How can this be fixed? Is there any better way to achieve this without using for..in?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can get only the keys using Object.keys

const keys = Object.keys(mapping);

keys.forEach((key) => {
  if(key === stringToBeMatched || mapping[key].linked_topics.includes(stringToBeMatched) ) {
    console.log(`found ${stringToBeMatched} in ${key}`);
  }
})
about 4 years ago · Juan Pablo Isaza Denunciar

0

Use entries as suggested by ESLint. I used ?. instead of . after "linked_topics" property to prevent Cannot read properties of undefined (reading 'includes') error when no property exists named linked_topics.

const stringToBeMatched = 'Simplexity' 

Object.entries(mapping).forEach(([key, value]) => {
  if(key === stringToBeMatched || value.linked_topics?.includes(stringToBeMatched) ) {
    console.log(`found ${stringToBeMatched} in ${key}`);
  }
})
about 4 years ago · Juan Pablo Isaza Denunciar

0

Good answer from @doctorgu, you could also you some.

const string = 'Simplexity'

const mapping = {
  'Mind Management': {
    type: 'average',
    linked_topics: ['Digital Detox', 'Small Scaling', 'Simplexity'],
    edge: 'Zeroed Out'
  },
  'Ancient Wisdom': {
    type: 'direct',
    edge: 'Roots Revival'
  }
}

const isStringInMapping = Object.entries(mapping).some(([key, value]) => (
    key == string || value.linked_topics?.includes(string)
))

console.log(isStringInMapping)

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda