0

I am declaring a variable as an array of key-value pairs in my Javscript file to use it as a Dataframe of sorts, but I noticed that when I put the object in console.log() the numbers it shows are totally incorrect, and additionally those displayed initially are different when the object is drilled down.

I cannot post images yet, so I've copied what my console log is displaying:

0: {Date: 'Jan-16', Pan y cereales: 4.3545866735289005, Carne: 6.950548997443754, Pescado: 0.8107680678589957, Leche, queso y huevos: 3.39456864737088, …}
1: {Date: 'Feb-16', Pan y cereales: 4.350266647067066, Carne: 6.950548997443754, Pescado: 0.825132069061222, Leche, queso y huevos: 3.39456864737088, …}
2: {Date: 'Mar-16', Pan y cereales: 4.350266647067066, Carne: 6.957124729702452, Pescado: 0.8434860705974001, Leche, queso y huevos: 3.404813824375017, …}
3: {Date: 'Apr-16', Pan y cereales: 4.3545866735289005, Carne: 6.950548997443754, Pescado: 0.8339100697959159, Leche, queso y huevos: 3.4082288833763963, …}
4: {Date: 'May-16', Pan y cereales: 4.3545866735289005, Carne: 6.943973265185055, Pescado: 0.8107680678589957, Leche, queso y huevos: 3.4082288833763963, …}

... and so on.

When drilling down, the numbers are different:

Aceites y grasas: 0.945930532678962
Adquisición de Vehículos: 2.0880989256158005
Aguas minerales, refrescos, jugos de frutas y de legumbres: 1.05780585516191
Alquileres Efectivos del Alojamiento: 2.255284623611528
Artefactos para el Hogar: 1.1245739404299515
Artículos de Vidrio y Cristal, Vajilla y Utensilios para el Hogar: 0.23502455461677574
Azúcar, mermelada, miel, chocolate y dulces de azúcar: 0.5792218843060288
Bebidas destiladas: 0.06329991325766103
Bienes y Servicios para Conservación Ordinaria del Hogar: 4.655659961216458
Café, té y cacao: 0.6912070763056545

... and so on.

I tried waiting for the page to load using functions and waiting manually by printing with a button press, but still, the numbers are wrong.

I assume that this may have to do with the size of the object, because I am not having the issue with smaller objects, but I have not been able to find a conclusive answer to this. Given its my only way of using this data, I am hoping the problem is fixable.

The object is below. I have shortened the object to fit it in here, but it is an array of 90 key-value pairs of 53 pairs each.

const inflacionJson = [
  {
    Date: "Jan-16",
    "Pan y cereales": 100.8,
    Carne: 105.7,
    Pescado: 101.6,
    "Leche, queso y huevos": 99.4,
    "Aceites y grasas": 98.1,
    Frutas: 101.7,
    "Legumbres-Hortalizas": 102.9,
    "Azúcar, mermelada, miel, chocolate y dulces de azúcar": 100.5,
    "Productos alimenticios, n.e.p.": 102.9,
    "Café, té y cacao": 99.9,
    "Aguas minerales, refrescos, jugos de frutas y de legumbres": 100.2,
    "Bebidas destiladas": 100,
    Vino: 101.9,
    Cerveza: 111.9,
    Tabaco: 100.2,
    "Prendas de Vestir": 97.1,
    Calzado: 95.9,
    "Alquileres Efectivos del Alojamiento": 100.9,
    "Conservación y Reparación de la Vivienda": 102.6,
    "Suministro de Agua y Servicios Diversos Relacionados con la Vivienda": 99.5,
    "Otros servicios relacionados con la vivienda, n.e.p.": 100.6,
    "Electricidad y Gas": 94.1,
    "Muebles y Accesorios": 100,
    "Productos Textiles para el Hogar": 93.9,
    "Artefactos para el Hogar": 99.5,
    "Artículos de Vidrio y Cristal, Vajilla y Utensilios para el Hogar": 100.2,
    "Herramientas y Equipo para el Hogar y el Jardín": 100.5,
    "Bienes y Servicios para Conservación Ordinaria del Hogar": 102.2,
    "Productos, Artefactos y Equipo Médico": 102.6,
    "Servicios para Pacientes Externos": 102.3,
    "Servicios de Hospital": 104.2,
    "Adquisición de Vehículos": 98,
    "Funcionamiento de Equipo de Transporte Personal": 96.6,
    "Servicios de Transporte": 102.3,
    "Equipo Telefónico": 130.3,
    "Servicios Telefónicos": 108.1,
    "Equipo Audiovisual, Fotográfico y de Procesamiento de Información": 94.1,
    "Otros Artículos y Equipo para Recreación, Jardines y Animales Domésticos": 100.8,
    "Servicios de Recreación y Culturales": 102.2,
    "Periódicos, Libros,  Papeles y Útiles de Oficina": 103,
    "Paquetes Turísticos": 98.4,
    "Enseñanza Preescolar y Primaria": 108.9,
    "Enseñanza Secundaria": 106.5,
    "Enseñanza Terciaria": 106.2,
    "Enseñanza no Atribuible a Ningún Nivel": 102.8,
    "Servicios de Restaurantes, Café y Establecimientos Similares": 124.3,
    "Servicios de Alojamiento": 108.9,
    "Cuidado Personal": 103.9,
    "Efectos Personales, n.e.p.": 101.9,
    "Protección Social": 105,
    Seguros: 101.4,
    "Servicios Financieros, n.e.p.": 100.3,
    "Otros Servicios, n.e.p.": 100.2,
  },
  {
    Date: "Feb-16",
    "Pan y cereales": 100.7,
    Carne: 105.7,
    Pescado: 103.4,
    "Leche, queso y huevos": 99.4,
    "Aceites y grasas": 97.8,
    Frutas: 100.9,
    "Legumbres-Hortalizas": 104.5,
    "Azúcar, mermelada, miel, chocolate y dulces de azúcar": 101,
    "Productos alimenticios, n.e.p.": 103.1,
    "Café, té y cacao": 99.9,
    "Aguas minerales, refrescos, jugos de frutas y de legumbres": 99.8,
    "Bebidas destiladas": 97.2,
    Vino: 101.9,
    Cerveza: 112.2,
    Tabaco: 103.6,
    "Prendas de Vestir": 97.4,
    Calzado: 96.2,
    "Alquileres Efectivos del Alojamiento": 100.9,
    "Conservación y Reparación de la Vivienda": 102.5,
    "Suministro de Agua y Servicios Diversos Relacionados con la Vivienda": 99.9,
    "Otros servicios relacionados con la vivienda, n.e.p.": 102,
    "Electricidad y Gas": 94,
    "Muebles y Accesorios": 100,
    "Productos Textiles para el Hogar": 93.8,
    "Artefactos para el Hogar": 99.2,
    "Artículos de Vidrio y Cristal, Vajilla y Utensilios para el Hogar": 100.2,
    "Herramientas y Equipo para el Hogar y el Jardín": 100.6,
    "Bienes y Servicios para Conservación Ordinaria del Hogar": 104.6,
    "Productos, Artefactos y Equipo Médico": 102.7,
    "Servicios para Pacientes Externos": 102.3,
    "Servicios de Hospital": 104.2,
    "Adquisición de Vehículos": 98.8,
    "Funcionamiento de Equipo de Transporte Personal": 92.1,
    "Servicios de Transporte": 103,
    "Equipo Telefónico": 130.5,
    "Servicios Telefónicos": 108.1,
    "Equipo Audiovisual, Fotográfico y de Procesamiento de Información": 93.8,
    "Otros Artículos y Equipo para Recreación, Jardines y Animales Domésticos": 100.8,
    "Servicios de Recreación y Culturales": 102.3,
    "Periódicos, Libros,  Papeles y Útiles de Oficina": 104.1,
    "Paquetes Turísticos": 99.7,
    "Enseñanza Preescolar y Primaria": 113.7,
    "Enseñanza Secundaria": 108.5,
    "Enseñanza Terciaria": 106.2,
    "Enseñanza no Atribuible a Ningún Nivel": 102.8,
    "Servicios de Restaurantes, Café y Establecimientos Similares": 124.8,
    "Servicios de Alojamiento": 109.5,
    "Cuidado Personal": 104.2,
    "Efectos Personales, n.e.p.": 102.4,
    "Protección Social": 105,
    Seguros: 101.4,
    "Servicios Financieros, n.e.p.": 100.3,
    "Otros Servicios, n.e.p.": 100.2,
  },
  {
    Date: "Mar-16",
    "Pan y cereales": 100.7,
    Carne: 105.8,
    Pescado: 105.7,
    "Leche, queso y huevos": 99.7,
    "Aceites y grasas": 97.1,
    Frutas: 104.2,
    "Legumbres-Hortalizas": 105.1,
    "Azúcar, mermelada, miel, chocolate y dulces de azúcar": 101.2,
    "Productos alimenticios, n.e.p.": 103.3,
    "Café, té y cacao": 99.8,
    "Aguas minerales, refrescos, jugos de frutas y de legumbres": 99.9,
    "Bebidas destiladas": 99.9,
    Vino: 102.1,
    Cerveza: 112.4,
    Tabaco: 107,
    "Prendas de Vestir": 97.5,
    Calzado: 96.1,
    "Alquileres Efectivos del Alojamiento": 100.9,
    "Conservación y Reparación de la Vivienda": 102.8,
    "Suministro de Agua y Servicios Diversos Relacionados con la Vivienda": 99.9,
    "Otros servicios relacionados con la vivienda, n.e.p.": 102,
    "Electricidad y Gas": 94,
    "Muebles y Accesorios": 100,
    "Productos Textiles para el Hogar": 93.5,
    "Artefactos para el Hogar": 99,
    "Artículos de Vidrio y Cristal, Vajilla y Utensilios para el Hogar": 100.2,
    "Herramientas y Equipo para el Hogar y el Jardín": 100.6,
    "Bienes y Servicios para Conservación Ordinaria del Hogar": 104.9,
    "Productos, Artefactos y Equipo Médico": 103.8,
    "Servicios para Pacientes Externos": 105.3,
    "Servicios de Hospital": 107.8,
    "Adquisición de Vehículos": 98.8,
    "Funcionamiento de Equipo de Transporte Personal": 95.9,
    "Servicios de Transporte": 100.8,
    "Equipo Telefónico": 127.8,
    "Servicios Telefónicos": 107,
    "Equipo Audiovisual, Fotográfico y de Procesamiento de Información": 93.1,
    "Otros Artículos y Equipo para Recreación, Jardines y Animales Domésticos": 100.9,
    "Servicios de Recreación y Culturales": 102.4,
    "Periódicos, Libros,  Papeles y Útiles de Oficina": 104.2,
    "Paquetes Turísticos": 99.7,
    "Enseñanza Preescolar y Primaria": 113.7,
    "Enseñanza Secundaria": 108.5,
    "Enseñanza Terciaria": 106.2,
    "Enseñanza no Atribuible a Ningún Nivel": 106.8,
    "Servicios de Restaurantes, Café y Establecimientos Similares": 125.4,
    "Servicios de Alojamiento": 109.5,
    "Cuidado Personal": 104.8,
    "Efectos Personales, n.e.p.": 102.4,
    "Protección Social": 109.7,
    Seguros: 102.2,
    "Servicios Financieros, n.e.p.": 100.3,
    "Otros Servicios, n.e.p.": 100.9,
  },

... and so on.

Any help would be appreciated!

kv005
  • 1
  • 2
  • 2
    The size of the object is *extremely* unlikely to be relevant. Can you please include both the code that logs this data (i.e. what code is doing the "drilling down"), and the mechanism by which the data is retrieved before logging? Usually this sort of thing is due to trying to log something that hasn't yet finished loading asynchronously. But since all you've given us here is a javascript object we can't have any idea what's going wrong in the code that uses it. – Daniel Beck Jul 21 '23 at 20:40
  • I'm also not able to see how the contents of "here's what my console log is showing" relate at all to the "when drilling down" data. What would be your expected output? I have no idea from the information given here – Daniel Beck Jul 21 '23 at 20:44
  • I just copy pasted your data into my console and everything looks fine to me. The keys of course are sorted alphabetically if you drill down. – twalow Jul 21 '23 at 20:45
  • @DanielBeck Thank you for your response. I apologize, I didn't use the right term. By drilling down I mean when printing an object such as this one in the console log, one has the option to click on a little arrow to view the other objects inside the larger object. The variable is declared outside of a function. The code executing the console.log() function is as follows: ``` function test() { console.log(inflacionJson); } ``` – kv005 Jul 21 '23 at 20:52
  • @twalow Thank you for your response. I tried launching the program from a live server on my computer and it works fine, so I believe this problem is maybe related to the hosting service? I am using Glitch.com. – kv005 Jul 21 '23 at 20:58
  • When console shows different values when you expand the object, this is an indication that the object was altered after it was logged, by code that ran after `console.log`. This usually is asynchronous executing code, like the code that runs when an AJAX response comes in. – trincot Jul 21 '23 at 21:57
  • "*those displayed initially are different when the object is drilled down*" - then see https://stackoverflow.com/questions/23392111/console-log-async-or-sync The problem is with some of your code that is modifying the object (which maybe it shouldn't do). Showing us the log outputs does not help, you'll need to post your code if you need help finding the problem – Bergi Jul 22 '23 at 00:37
  • 1
    @Bergi Thank you. I found that remove all functions fixed it. So these functions were executing before the console.log() even though they were later in the code. I am new to Javascript, so I assume what is happening has to do with async/sync functions as you mentioned. – kv005 Jul 24 '23 at 14:06

0 Answers0