I have the following function where it iterates the group of tags objects to be able to consult a data of each object with a library,
//Esta es la lista a iterar
var tags = ["Contador_Turno", "Contador_Pedido", "Paquetes", "Tarimas"]
readTag(plc) {
const json = [];
this.tags.forEach(tag => {
//Aqui se itera cada tag (por ejemplo "Contador_Turno" y se utiliza una libreria para consultar el valor de cada tag)
plc.read(`${tag}`).then(data => {
//Aqui es donde trato de asignar los valores correspondientes de cada tag para formar un array json
json.push(tag, data)
console.log(json)
})
});
}
The output of that function in the console is the following:
[ 'Contador_Pedido', 66 ]
[ 'Contador_Pedido', 66, 'Paquetes', 1 ]
[ 'Contador_Pedido', 66, 'Paquetes', 1, 'Contador_Turno', 66 ]
[
'Contador_Pedido',
66,
'Paquetes',
1,
'Contador_Turno',
66,
'Tarimas',
0
]
I am trying to make the json look like this so that each tag has its corresponding value
[{'Contador_Pedido': 66, 'Paquetes': 1, 'Contador_Turno': 66, 'Tarimas': 0}]
I would be grateful if you could help me to see where the error is
To create objects within an array it would be
You only needed to add the opening and closing symbols of an object.
Tell us if it worked for you
You could use map instead of foreach: