Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Calculadora

0

54
Vistas
Get the key of a JS Object from the value

I am trying to get the key of a JS Object in Typescript from an input value, the problem is that the values are inside an Array.

This is what I have seen in the case that the value is not in an array:

const exampleObject = {
  key1: 'Geeks',
  key2: 'Javascript'
};

function getKeyByValue(object, value) {
  for (var prop in object) {
      if (object.hasOwnProperty(prop)) {
          if (object[prop] === value)
          return prop;
      }
  }
}

console.log(getKeyByValue(exampleObject,'Geeks')) // key1

This is an example of my object that I want to get the key from:

const exampleObject = {
  key1: ['Geeks','test1','test2'],
  key2: ['Javascript','test3','test4']
};

function getKeyByValue(object, value) {
  for (var prop in object) {
      if (object.hasOwnProperty(prop)) {
          if (object[prop] === value)
          return prop;
      }
  }
}

console.log(getKeyByValue(exampleObject,'Geeks')) // undefined

I need to get the key without using Array.prototype.includes(), because typing the resulting variable as String gives me an error that it may be undefined.

My problem: I don't know how to go through the array inside the function to find the value and get the key

Update:

What I want is to avoid the possibility of returning an undefined, since the input value will always be inside the object, how can I achieve this?

7 months ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

@segmet You can use my code

const exampleObject = {
    key1: ['Geeks', 'test1', 'test2'],
    key2: ['Javascript', 'test3', 'test4']
};

function getKeyByValue(object, value) {
    var output = "";
    for (var prop in object) {
    // finding and removing element from array
        object[prop].find(i => {
                if (i === value) {
                    output = prop;
                    return prop;
                }
            }
        )
    }
    return output
}

console.log(getKeyByValue(exampleObject, 'Geeks'))
7 months ago · Juan Pablo Isaza Denunciar

0

function getKeyByValue(object, value) {
  const key = Object.entries(object).filter(([key, val]) => val.includes(value));
  return Object.fromEntries(key);
}

Try this function instead You will get the object matching your keys

7 months ago · Juan Pablo Isaza Denunciar

0

You can do something like this and then check for null


const getKeyFromValue = (obj:Object, value:string | string[]) => { 
    for (let key in obj) {
        if (typeof value === 'string') {
            if (obj[ key ].includes(value)) {
                return key;
            }
        }
        else if (Array.isArray(value)) { 
            if (obj[ key ].every(val => value.includes(val))) {
                return key;
            }
        }
       
    }
    return null;
}
7 months 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 empleo Planes Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2023 PeakU Inc. All Rights Reserved.