javascript - add array of object into array of object using forEach es6 -


i have set of colors array ["#c4b18f", "#100f5c"]

and want add each cameras, cameras nested array, so

[   {     "id": 1,     "date": "sat",     "cameras": [       {         "name": "east gate",         "total_count": 233       },       {         "name": "south gate",         "total_count": 2599       }     ]   },   {     "id": 2,     "date": "sun",     "cameras": [       {         "name": "east gate",         "total_count": 123342       },       {         "name": "south gate",         "total_count": 2333       }     ]   } ] 

i've tried got undefined

const result = data.foreach(obj => ({                 ...obj,                 cameras: obj.cameras.map((obj2, i) => ({                     ...obj2,                     color: colors[i]                 }))             })) 

i suspect forgot return something, or should use map instead of foreach first loop?

you can use loop. code below help. let arr initial array.

    for(var j=0;j<2;j++){         var cameras = arr[j]["cameras"];         for(var i=0;i<2;i++){             cameras[i]["color"]=["#c4b18f", "#100f5c"]         }     } 

Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -