• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

282
Views
Cómo combinar objetos JSON complicados en Javascript

Estoy haciendo un visor de red usando Cytoscape JS, para lo cual necesito darle estilo a través de un objeto JSON. Básicamente, quiero un mapa de calor, así que los selecciono en 255 categorías, cada una con su propio color. Y como no voy a escribir 255 entradas, quería hacerlo con un bucle. Sin embargo, la combinación me está dando dolor de cabeza y realmente no puedo entender dónde estoy siendo estúpido.

 var create_stylesheet = function(){ var to_return =[ { 'selector': 'node', 'style': {'content': 'data(label)', 'background-color': 'BlueViolet'} }, { 'selector': 'edge', 'style': {'label': 'data(score)'} }]; // <- this entry is for basic node labels and stuff. It needs to be first. //For loop that assigns a colour to each category. //As the red RGB value goes up, the blue RGB goes down. creating a gradient from cold to warm. // In the program warmer colours have a higher score. for(i = 0; i <= 255; i++){ var cat = `.cat_${i}`; var colour = `rgb(${i}, 0, ${255-i})`; var to_append = { 'selector': cat, 'style': {'line-color': colour} }; //Here I'd like to add to_append to the to_return variable. } return to_return; //Return the finished stylesheet. }

Gracias, realmente aprecio la ayuda.

EDITAR: Gracias a las personas que piensan junto conmigo. Lo que hice mal fue tratar de usar varias formas de hacerlo a la vez, lo que obviamente no funcionó. Así que construyo todo bien y lentamente, la ineficiencia les dará noches de insomnio a ciertos programadores, pero aquí está el código de trabajo:

 var create_stylesheet = function(){ let to_return = []; let first_line = { 'selector': 'node', 'style': {'content': 'data(label)', 'background-color': 'BlueViolet'} }; let second_line = { 'selector': 'edge', 'style': {'label': 'data(score)'} }; let last_line = { 'selector': ':selected', 'style': { 'background-color': 'SteelBlue', 'line-color': 'black', 'target-arrow-color': 'black', 'source-arrow-color': 'black'} }; //Push the first two line into the array. to_return.push(first_line); to_return.push(second_line); for(let i = 0; i <= 255; i++){ var cat = `.cat_${i}`; var colour = `rgb(${i}, 0, ${255-i})`; var to_append = { 'selector': cat, 'style': {'line-color': colour} }; to_return.push(to_append); //Push each line into the array. } to_return.push(last_line); //add the last line. return to_return; }
over 3 years ago · Santiago Gelvez
1 answers
Answer question

0

Dos cosas a tener en cuenta.

  1. En el ciclo for, necesitas let i = 0 .
  2. Entonces, puedes simplemente concatenar y regresar.
 var create_stylesheet = function(){ var to_return =[ { 'selector': 'node', 'style': {'content': 'data(label)', 'background-color': 'BlueViolet'} }, { 'selector': 'edge', 'style': {'label': 'data(score)'} }]; // <- this entry is for basic node labels and stuff. It needs to be first. //For loop that assigns a colour to each category. //As the red RGB value goes up, the blue RGB goes down. creating a gradient from cold to warm. // In the program warmer colours have a higher score. for(let i = 0; i <= 255; i++){ var cat = `.cat_${i}`; var colour = `rgb(${i}, 0, ${255-i})`; var to_append = { 'selector': cat, 'style': {'line-color': colour} }; to_return = to_return.concat(to_append); } return to_return; } create_stylesheet()
over 3 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error