[removed] Accessing Property of Objects Inside an Array

前端 未结 3 2036
醉话见心
醉话见心 2021-01-23 04:40

How can I get the integer \"100\" from dataset1 [below]?


    var dataset1 = [ 
            {video_name: \"Apples\", video_views: 100},
            {video_name: \"Or         


        
3条回答
  •  佛祖请我去吃肉
    2021-01-23 04:48

    You are working with an array of objects here. It should, therefore, be possible to use the forEach() method of arrays in javascript. You can read more about this in MD5 Documentation

    The following code snippet should iterate through your array of objects and access the key: value pairs easily.

    var dataset1 = [ 
                {video_name: "Apples", video_views: 100},
                {video_name: "Oranges", video_views: 35},
                {video_name: "Grapes", video_views: 20},
                {video_name: "Avocados", video_views: 85},
                {video_name: "Tomatoes", video_views: 60}
            ]
    
    dataset1.forEach(function (element, index, array){
    console.log("Video Name: " + dataset1[index].video_name + "; Views: "+ dataset1[index].video_views);
    });

提交回复
热议问题