I have an array of objects let's call it 1 and another object let's call it 2 and I need to compare the Array of objects 1 with object 2. If the first element of objects 2 is equal to the name attribute of the array objects 1 then we replace the first element in objects 2 for that name_common for example element 4_X_X_30_last first object 2 I look for it in the array of objects 1 "name":"4_X_X_30_last" if it is the same I replace 4_X_X_30_last with the value of name_common Panel_solar. At the end I put an example of how the result should look. In addition to that, I need to put in that same element a new metadata that is that id element that we have replaced as it appears in the example.
I'm trying to do several nested for looping through the objects and then looking for the ID but since object 2 has no label I don't know how.
I have an object of such form:
{
"4_X_X_30_last":{
"type":"float",
"value":6456,
"metadata":{
"timestamp":{
"type":"Integer",
"value":"1565006400"
}
}
},
"7_X_X_7_last":{
"type":"float",
"value":6183,
"metadata":{
"timestamp":{
"type":"Integer",
"value":"1565006400"
}
}
},
"11_X_X_600_avg":{
"type":"float",
"value":332,
"metadata":{
"timestamp":{
"type":"Integer",
"value":"1565006400"
}
}
}
}
and an array of objects:
[
{
"name":"11_X_X_600_avg",
"type":"clima",
"place":"interior",
"img":"assets/img/contrast.png",
"name_comun":"Radiacion_solar",
"medida":"W/m2",
"interfaz":""
},
{
"name":"7_X_X_7_last",
"type":"resources",
"place":"interior",
"img":"assets/img/flash.png",
"name_comun":"Nivel_bateria",
"medida":"mV",
"interfaz":""
},
{
"name":"4_X_X_30_last",
"type":"resources",
"place":"interior",
"img":"assets/img/flash.png",
"name_comun":"Panel_solar",
"medida":"mV",
"interfaz":""
}
]
result:
{
"Panel_solar":{
"type":"float",
"value":6456,
"metadata":{
"timestamp":{
"type":"Integer",
"value":"1565006400"
},
"id":{
"type":"String",
"value":"4_X_X_30_last"
}
}
},
"Nivel_bateria":{
"type":"float",
"value":6183,
"metadata":{
"timestamp":{
"type":"Integer",
"value":"1565006400"
},
"id":{
"type":"String",
"value":"7_X_X_7_last"
}
}
},
"Radiacion_solar":{
"type":"float",
"value":332,
"metadata":{
"timestamp":{
"type":"Integer",
"value":"1565006400"
},
"id":{
"type":"String",
"value":"11_X_X_600_avg"
}
}
}
}
This is the code that I am testing but I am not sure how to replace the mentioned values without creating a new object with .push
var datos = [
{"name":"11_X_X_600_avg","type":"clima","place":"interior","img":"assets/img/contrast.png","name_comun":"Radiacion_solar","medida":"W/m2","interfaz":""},
{"name":"7_X_X_7_last","type":"resources","place":"interior","img":"assets/img/flash.png","name_comun":"Nivel_bateria","medida":"mV","interfaz":""},
{"name":"4_X_X_30_last","type":"resources","place":"interior","img":"assets/img/flash.png","name_comun":"Panel_solar","medida":"mV","interfaz":""}];
var Fiware = {"4_X_X_30_last":{"type":"float","value":6456,"metadata":{"timestamp":{"type":"Integer","value":"1565006400"}}},"7_X_X_7_last":{"type":"float","value":6183,"metadata":{"timestamp":{"type":"Integer","value":"1565006400"}}},"11_X_X_600_avg":{"type":"float","value":332,"metadata":{"timestamp":{"type":"Integer","value":"1565006400"}}}};
for(var propName in Fiware) {
for (var i = 0; i< datos.length; i++){
if (datos[i].name === propName){
console.log("Nombre propiedad: ", propName, " y sus valores son: ", Fiware[propName])
}
}
}
In order to obtain a result as desired, the solution is to rename keys in the object. You can read about it at this link: https://medium.com/@yazeedb/immutably-rename-object-keys-in-javascript-5f6353c7b6dd
Here is an implementation of your example of how it would be applied using the reduce function: