0
I am learning Javascript and am creating a script that needs to put a Json file in alphabetical order and print on the screen if you have repeated item and if it exists show which.
I have looked at several questions and so far I could not find a conclusion, if someone help me I will be very grateful
This is my HTML script:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="container">
<ul id="data"></ul>
<div id="app"></div>"
<button onClick="console.log(dados, uniqNames)"> Rotate </button>
</div>
<script type="text/javascript">
const dados = {
"alimentos":
{
"carne": "carne",
"arroz": "feijão",
},
"bebidas":
{
"agua": "gelada",
"leite": "nescau",
},
}
// Pega os nome
const names = dados.alimentos.map( identificar => identificar.CLIENT_ID )
const names = dados.bebidas.map( bebidas => bebidas.CLIENT_ID )
// Filtrar nomes repetidos
const uniqNames = names.filter((name, index, self) => self.indexOf(name) === index)
//Coloca em ordem alfabetica
uniqNames.sort(function (a, b) {
return (a.nome > b.nome) ? 1 : ((b.nome > a.nome) ? -1 : 0);
});
//Passa os nomes verificados
let resultados = [];
uniqNames.forEach( uName => {
names.forEach( name => {
if (name == uName) {
resultados[uName] = (resultados[uName] > 0 ? resultados[uName] : 0) + 1
}
})
})
console.log(dados, uniqNames)
</script>
</body>
</html>
the expected result is put Json in alphabetical order translation car:cars milk:milk
translation 2 love:love car:cars
(Print message that is repeated) message: car:car is repeated
<script>
const dados = {
"tradução":
{
"car": "carros",
"leite": "milk",
},
"tradução_2":
{
"car": "carros",
"amor": "love",
},
}
// Pega os nome
const names = dados.tradução.map( tradução => tradução.CLIENT_ID )
// Filtrar nomes repetidos
const uniqNames = names.filter((name, index, self) => self.indexOf(name) === index)
//Coloca em ordem alfabetica
uniqNames.sort(function (a, b) {
return (a.nome > b.nome) ? 1 : ((b.nome > a.nome) ? -1 : 0);
});
//Passa os nomes verificados
let resultados = [];
uniqNames.forEach( uName => {
names.forEach( name => {
if (name == uName) {
resultados[uName] = (resultados[uName] > 0 ? resultados[uName] : 0) + 1
}
})
})
console.log(dados, uniqNames)
</scrtipt>
The "Json file" you want to sort is the data variable? Why is it in this format? It would not be better to do so: data ={"food": ["meat", "rice", "beans"], "drinks": ["water", "milk"]}
– Artur Brasil
yes this way I did and it worked, however I need to do in a specific Json that is in this format
– glen
The shape that is in the question makes no sense. An object has key and value. The way you put it is key "meat", value "meat", then key "rice", value "beans". Agree that makes no sense? Why do you say it needs to be like this? Another error in your code is just below, you are declaring
names
twice and trying to access a property that doesn’t exist: CLIENT_ID– Artur Brasil
this Json is a huge list I just put this example by format, because it is a translation Json so for example it ta key"car": value "car" I need to print the key and the value not only the value, and relation to Names I fix but it only takes the "food" would like to read tbm "drink" that’s the doubt tbm
– glen
What is the expected result? Please create a [mcve], you don’t need all this code to demonstrate the problem. For example, the
<head>
HTML does not influence your question at all...– Rafael Tavares
There is no way to sort object keys, IE, it is not possible to do what you want and keep the structure of this json.
– bfavaretto