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

87
Vistas
How do I grab specific data dynamically from an array of objects?

Situation

I have code that takes a local JSON file and parses it into an object. This part works perfectly.

My goal is to create a function that takes two parameters:

  1. The path of the JSON file to be parsed.
  2. The property name of a specific object so I can grab the value behind the key.

Problem

I added a variable (objectName) to pass the property name. I also added [0].fx so I can call the data from the first object only. When I run my code, I get Undefined.

Where did I go wrong?

(in the example code below, I was aiming to get "David")

const localDataObject = (fileName, objectName) => {
const localData = path.join(__dirname, "../", fileName);

fs.readFile(localData, (err, data) => {
   const objectData = JSON.parse(data);
   const fx = objectName;
        console.log(objectData[0].fx);
    });
};

localDataObject("./Family.json", "name");

the first object looks like this:

[
    {
        "name": "David",
        "age": "24",
        "role": "Studet"
    },

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

0

The solution for your code is :

const localDataObject = (fileName, objectName) => {
const localData = path.join(__dirname, "../", fileName);

fs.readFile(localData, (err, data) => {
   const objectData = JSON.parse(data);
   const fx = objectName;
        console.log(objectData[0][fx]);
    });
};

localDataObject("./Family.json", "name");
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