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

151
Views
Insertar árbol de objetos en matriz/objeto por valor

Quiero usar chart.js

La estructura no está completa, pero esto no importa, creo. Y tengo una estructura prototipo para yAxes. Estos deben insertarse 3x en chartDef.options.scales.yAxes pero POR VALOR para que pueda cambiar las tres partes de forma independiente. y se debe insertar el ID. Pero haciendo como en mi código, las tres partes se cambian al mismo (último) número.

Para mí, parece agregar por valor / por problema de referencia.

¿Cómo puedo resolver?

Gracias

 var yaxes_prototype ={ ticks: { autoSkip: true, maxTicksLimit: 20, min: -1, max: 1, stepSize: 0.2 } } var chartDef = { type: 'line', data: { datasets: [] }, options: { responsive: true, showTooltips: true, hoverMode: 'index', stacked: false, scales: { xAxes: [], yAxes: [] }, } } console.log("yaxes_prototype",yaxes_prototype) for (var i=0 ; i<3 ; i++){ //##### OK, but BY REFERENCE chartDef.options.scales.yAxes.push(yaxes_prototype); // OK ,insert as newobject chartDef.options.scales.yAxes[i]["id"]=i // key,:val was added, but all val the same } console.log("chartDef",chartDef.options.scales)
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

El problema es que envías el yaxes_prototype exactamente igual tres veces.

Este fragmento a continuación puede ayudarlo a ver si el obj1 enviado como parámetro se recibe exactamente en el mismo objeto (por referencia), que si cambia alguno de ellos, todo cambiará.

 var obj1 = {name: "foo", value: "bar"}; (function() { if ( typeof Object.prototype.uniqueId == "undefined" ) { var id = 0; Object.prototype.uniqueId = function() { if ( typeof this.__uniqueid == "undefined" ) { this.__uniqueid = ++id; } return this.__uniqueid; }; } })(); function printId(obj) { console.log(obj.uniqueId()); } for(var i=0; i<3; i++) printId(obj1);

Por lo tanto, debe copiar eso y volver a enviarlo como una variable/objeto diferente en el bucle:

 var yaxes_prototype ={ ticks: { autoSkip: true, maxTicksLimit: 20, min: -1, max: 1, stepSize: 0.2 } } console.log("yaxes_prototype",yaxes_prototype) for (var i=0 ; i<3 ; i++){ var copyObj = {...yaxes_prototype}; // This copies the value stored in yaxes_prototype chartDef.options.scales.yAxes.push(copyObj); // OK ,insert as newobject chartDef.options.scales.yAxes[i]["id"]=i // key,:val was added, but all val the same }

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!