Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

106
Views
Cómo crear los datos como una matriz de objetos con un límite de 5 datos en JavaScript

Cómo crear los datos como una matriz de objetos con un límite de 5 datos en JavaScript.

Estoy usando web socket y recibo esta llamada por primera vez

 [ {city:"Newyork",value:10,"time":"13:8"}, {city:"London",value:20,"time":"13:8"} ]

Ahora en la segunda llamada, obtendré datos como ese

 [ {city:"Newyork",value:150,"time":"13:20"}, {city:"London",value:25,"time":"13:20"} {city:"Sydney",value:20,"time":"13:20"} ]

Entonces, lo que estoy haciendo aquí es agregar nuevos datos y actualizar los datos existentes y luego actualizar mi estado. Aquí está el código para eso.

 var city = this.state.city; for(var j=0; j<res.length; j++){ const i = city.findIndex(_item => _item.city === res[j].city); if (i > -1) city[i] = res[j]; // (2) else city.push(res[j]); } this.setState({data:city})

Esto está funcionando bien.

Ahora, cómo declaro un estado en el que tengo que actualizar cinco datos que se basan en las últimas cinco llamadas de WebSocket.

Algo así

 myData = [{newyork:{[value:10,time:13:20],[value:150,time:13:20]}},London:{[value:20,time:13:20],[value:25,time:13:20]},Sydney:{[value:20,time:13:20]}];

Entonces, en base a estos datos, puedo trazar un gráfico de una ciudad específica. Necesito solo 5 datos de cada ciudad.

¿Me puede ayudar alguien?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

En cada actualización, para cada ciudad, puedes hacer algo como esto:

 if(city[city_name].length >= 5){ city[city_name].shift(); } city[city_name].push(new_city);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!