My object looks like this:
I want to add the key value that contains the arrays as a value in the object to achieve something like this:
{avg: 16, day: "2020-12-11", max: 25, min: 8,value:"o3"}
{avg: 18, day: "2020-12-12", max: 21, min: 15,value:"o3"}
{avg: 10, day: "2020-12-13", max: 16, min: 6,value:"o3"}
{avg: 16, day: "2020-12-11", max: 19, min: 12, value: "pm10"}
{avg: 11, day: "2020-12-12", max: 17, min: 6, value: "pm10"}
{avg: 20, day: "2020-12-13", max: 26, min: 10, poluant: "pm10"}
I tried to loop through each object and to add the value at the end like this and works but i wonder if i can do this without to loop through each object(in the end i have five for loops):
for (let item of this.o3) {
item.value = 'o3';
}
for (let item of this.pm10) {
item.value = 'pm10';
}
Jou can do it by using loop inside a loop
var daily = { o3: [{avg: 16, day: "2020-12-11", max: 25, min: 8}, {avg: 18, day: "2020-12-12", max: 21, min: 15}, {avg: 10, day: "2020-12-13", max: 16, min: 6}, {avg: 16, day: "2020-12-11", max: 19, min: 12}, {avg: 11, day: "2020-12-12", max: 17, min: 6}, {avg: 20, day: "2020-12-13", max: 26, min: 10} ], pm10: [{avg: 16, day: "2020-12-11", max: 25, min: 8}, {avg: 18, day: "2020-12-12", max: 21, min: 15}, {avg: 10, day: "2020-12-13", max: 16, min: 6}, {avg: 16, day: "2020-12-11", max: 19, min: 12}, {avg: 11, day: "2020-12-12", max: 17, min: 6}, {avg: 20, day: "2020-12-13", max: 26, min: 10 } ]}; for(item in daily){ if(item == "o3"){ daily[item] = daily[item].map(x=> x = {...x, value:"o3"} ) }else if("pm10"){ daily[item] = daily[item].map(x=> x = {...x, value:"pm10"} ) } } console.log(daily)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.