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

102
Vistas
How to interate a json string and save it to an array in javascript

I have data sent from a php page and I want to save the 'date' and 'count' values into two different arrays in javascript. But I don't know how to access the data below. Any help?

{
  "0": {
    "date": "12/24/21",
    "count": 1
  },
  "1": {
    "date": "01/01/22",
    "count": 1
  },
  "3": {
    "date": "01/02/22",
    "count": 2
  },
  "6": {
    "date": "01/04/22",
    "count": 3
  },
  "7": {
    "date": "01/05/22",
    "count": 1
  },
  "9": {
    "date": "01/06/22",
    "count": 2
  }
}

javascript code:

 $(document).ready(function(){
    $.ajax({
        url: "/ajax/page",
        method: "GET",
        success: function(data) { 
        let date = [];
        let count = [];
        // code to save the values from php page

  },
   error: function(data) {
            console.log('error');
        }
    });
});
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I think you may be missing some parenthesis and curly brackets in there after the success field. You might wanna fix it up a lil like:

$(document).ready(function () {
  $.ajax({
    url: "/ajax/page",
    method: "GET",
    success: function (data) {
      let date = [];
      let count = [];
      // code to save the values from php page
      for (keys in data) {
        //do ya thang 
      }
    },
    error: function (data) {
      console.log("error");
    }
  });
});

With it all nice and spruced up, you could utilize a few methods for iterating through that JSON object that's returned. You could either get the objects keys and use them to iterate through the object with Object.keys(yourObject), or you could use a for-in loop like above to iterate through the object.

Either way, you can iterate through the object and push the date and count vals for each field in the object onto your date and count arrays.

about 4 years ago · Juan Pablo Isaza Denunciar

0

const data =
  '{ "0": { "date": "12/24/21", "count": 1 }, "1": { "date": "01/01/22", "count": 1 }, "3": { "date": "01/02/22", "count": 2 }, "6": { "date": "01/04/22", "count": 3 }, "7": { "date": "01/05/22", "count": 1 }, "9": { "date": "01/06/22", "count": 2 } }';
const obj = JSON.parse(data);

const dateArr = [];
const countArr = [];

for (const value of Object.values(obj)) {
  const { date, count } = value;
  dateArr.push(date);
  countArr.push(count);
}
console.log("dateArr", dateArr);
console.log("countArr", countArr);

this is quite straightforward, if your data is json, you need to parse it to Javascript first using JSON.parse, and then use Object.values and a loop to push your data to your variables.

Here I'm using for...of loop

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