Browse json multidimencional javascript

Asked

Viewed 278 times

0

I have this json [{"id":1,"id_pai":null,"id_usuario":1,"ordem":1,"lado":"1"},[{"id":2,"id_pai":1,"id_usuario":2,"ordem":1,"lado":"1"},{"id":3,"id_pai":1,"id_usuario":3,"ordem":2,"lado":"2"}],[{"id":4,"id_pai":2,"id_usuario":4,"ordem":1,"lado":"1"}],[{"id":2,"id_pai":1,"id_usuario":2,"ordem":1,"lado":"1"},{"id":3,"id_pai":1,"id_usuario":3,"ordem":2,"lado":"2"}]]

I need to go through and turn it into this, the missing data, I’ll add it later.

{ id: 0, parent: null, description: "Nivel do Peão", email: "[email protected]", groupTitleColor: "#4169e1", image: "demo/images/photos/q.png", itemTitleColor: "#4169e1", phone: "(19) 98111-9983", title: "Nome do Peão", label: "Nome do Peão" }

How do I navigate it with javascript?

Question 2, how do I not go through the duplicate items?

  • Isn’t it easier to modify your query that builds this data? Is your question about Jstree? https://answall.com/questions/43477/montar-%C3%A1rvore-jstree

  • Yes, in theory, the problem is that I am consuming this js of an api that returns me in the initial form, the second way, is which I need to leave to scroll in the data structure to run in the plugin I am using...

1 answer

0


Remember that these are json’s inside arrays: [{"id":1,"id_pai":null,"id_usuario":1,"ordem":1,"lado":"1"},[{"id":2,"id_pai":1,"id_usuario":2,"ordem":1,"lado":"1"},{"id":3,"id_pai":1,"id_usuario":3,"ordem":2,"lado":"2"}],[{"id":4,"id_pai":2,"id_usuario":4,"ordem":1,"lado":"1"}],[{"id":2,"id_pai":1,"id_usuario":2,"ordem":1,"lado":"1"},{"id":3,"id_pai":1,"id_usuario":3,"ordem":2,"lado":"2"}]]

Here’s what you can do!

When it’s array you can subscribe and get the final result! See:

			var text = [{"id":1,"id_pai":null,"id_usuario":1,"ordem":1,"lado":"1"},[{"id":2,"id_pai":1,"id_usuario":2,"ordem":1,"lado":"1"},{"id":3,"id_pai":1,"id_usuario":3,"ordem":2,"lado":"2"}],[{"id":4,"id_pai":2,"id_usuario":4,"ordem":1,"lado":"1"}],[{"id":2,"id_pai":1,"id_usuario":2,"ordem":1,"lado":"1"},{"id":3,"id_pai":1,"id_usuario":3,"ordem":2,"lado":"2"}]];

			text[0].cidade = "Cuiabá"; // Criando novo valor
			text[0].id = 99999; // Subscrevendo o valor antigo

			text = text; /* Subscreve o array de json antigos para o atual com mudanças */

			console.log(text); // Imprime no console do navegador

With JSON’s:

var text = {
"employees":[
    {"firstName":"John", "lastName":"Doe"}, 
    {"firstName":"Anna", "lastName":"Smith"},
    {"firstName":"Peter", "lastName":"Jones"}
]
};

// Mudando valores
text.employees[0].firstName = "Maicon";
text.employees[0].lastName = "Ferreira";
// Adicionando valores
text.employees[0].cidade = "Cuiabá";
text.employees[0].estado = "Mato Grosso";
text.employees[0].pais = "Brasil";

// Prescrevendo valores
text = text;
// Imprimindo no console
console.log(text);

If I helped, don’t forget to classify it as the correct answer!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.