迭代嵌套的JSON数组

时间:2015-03-06 19:51:49

标签: javascript arrays

我想把我的一些嵌套对象数组推送到一个新的JSON数组对象。

我希望每个频道都将已发布的曲目带到一个新的json obj,然后将它们合并到一个新的数组对象中。

我的JSON:

[
{
    "id": 1,
    "user_id": 1,
    "genre_id": null,
    "genre_id_opt": null,
    "name": "channela1",
    "slug": "",
    "description": null,
    "is_verified": 0,
    "cover": null,
    "avatar": null,
    "created_at": null,
    "updated_at": null,
    "publishedTracks": [
        {
            "id": 1,
            "album_id": 1,
            "name": "track1",
            "slug": "track1",
            "track_no": null,
            "track": null,
            "duration": 0,
            "avatar": "track_avatar1.jpg",
            "genre_id": null,
            "genre_id_opt": null,
            "is_explicit": null,
            "created_at": "2015-03-06T14:32:18.000Z",
            "updated_at": "2015-03-06T14:32:18.000Z",
            "album": {
                "id": 1,
                "channel_id": 1,
                "genre_id": null,
                "genre_id_opt": null,
                "name": "alb1",
                "slug": "alb1",
                "description": "labore qui non et et ratione nobis",
                "release_date": null,
                "avatar": "alb_avatar1.jpg",
                "type": null,
                "is_publish": 1,
                "created_at": "2015-03-06T14:32:18.000Z",
                "updated_at": "2015-03-06T14:32:18.000Z"
            },
            "_pivot_id": 1,
            "_pivot_channel_id": 1
        },
        {
            "id": 27,
            "album_id": 1,
            "name": "track27",
            "slug": "track27",
            "track_no": null,
            "track": null,
            "duration": 0,
            "avatar": "track_avatar27.jpg",
            "genre_id": null,
            "genre_id_opt": null,
            "is_explicit": null,
            "created_at": "2015-03-06T14:32:18.000Z",
            "updated_at": "2015-03-06T14:32:18.000Z",
            "album": {
                "id": 1,
                "channel_id": 1,
                "genre_id": null,
                "genre_id_opt": null,
                "name": "alb1",
                "slug": "alb1",
                "description": "qui non et et ratione sint officia nobis",
                "release_date": null,
                "avatar": "alb_avatar1.jpg",
                "type": null,
                "is_publish": 1,
                "created_at": "2015-03-06T14:32:18.000Z",
                "updated_at": "2015-03-06T14:32:18.000Z"
            },
            "_pivot_id": 1,
            "_pivot_channel_id": 1
        },
        {
            "id": 2,
            "album_id": 14,
            "name": "track2",
            "slug": "track2",
            "track_no": null,
            "track": null,
            "duration": 0,
            "avatar": "track_avatar2.jpg",
            "genre_id": null,
            "genre_id_opt": null,
            "is_explicit": null,
            "created_at": "2015-03-06T14:32:18.000Z",
            "updated_at": "2015-03-06T14:32:18.000Z",
            "album": {
                "id": 14,
                "channel_id": 1,
                "genre_id": null,
                "genre_id_opt": null,
                "name": "alb14",
                "slug": "alb14",
                "description": "aliquam odio sapiente architecto",
                "release_date": null,
                "avatar": "alb_avatar14.jpg",
                "type": null,
                "is_publish": 1,
                "created_at": "2015-03-06T14:32:18.000Z",
                "updated_at": "2015-03-06T14:32:18.000Z"
            },
            "_pivot_id": 14,
            "_pivot_channel_id": 1
        },
        {
            "id": 28,
            "album_id": 14,
            "name": "track28",
            "slug": "track28",
            "track_no": null,
            "track": null,
            "duration": 0,
            "avatar": "track_avatar28.jpg",
            "genre_id": null,
            "genre_id_opt": null,
            "is_explicit": null,
            "created_at": "2015-03-06T14:32:18.000Z",
            "updated_at": "2015-03-06T14:32:18.000Z",
            "album": {
                "id": 14,
                "channel_id": 1,
                "genre_id": null,
                "genre_id_opt": null,
                "name": "alb14",
                "slug": "alb14",
                "description": "aliquam odio sapiente architecto",
                "release_date": null,
                "avatar": "alb_avatar14.jpg",
                "type": null,
                "is_publish": 1,
                "created_at": "2015-03-06T14:32:18.000Z",
                "updated_at": "2015-03-06T14:32:18.000Z"
            },
            "_pivot_id": 14,
            "_pivot_channel_id": 1
        }
    ]
},
{
    "id": 2,
    "user_id": 1,
    "genre_id": null,
    "genre_id_opt": null,
    "name": "chann1",
    "slug": "chann1",
    "description": "adipisci non impedit tempora mollitia et est",
    "is_verified": 0,
    "cover": "chann_cover1.jpg",
    "avatar": "chann_avatar1.jpg",
    "created_at": "2015-03-06T14:32:18.000Z",
    "updated_at": "2015-03-06T14:32:18.000Z",
    "publishedTracks": [
        {
            "id": 3,
            "album_id": 2,
            "name": "track3",
            "slug": "track3",
            "track_no": null,
            "track": null,
            "duration": 0,
            "avatar": "track_avatar3.jpg",
            "genre_id": null,
            "genre_id_opt": null,
            "is_explicit": null,
            "created_at": "2015-03-06T14:32:18.000Z",
            "updated_at": "2015-03-06T14:32:18.000Z",
            "album": {
                "id": 2,
                "channel_id": 2,
                "genre_id": null,
                "genre_id_opt": null,
                "name": "alb2",
                "slug": "alb2",
                "description": "aliquid neque autem est dignissimos",
                "release_date": null,
                "avatar": "alb_avatar2.jpg",
                "type": null,
                "is_publish": 1,
                "created_at": "2015-03-06T14:32:18.000Z",
                "updated_at": "2015-03-06T14:32:18.000Z"
            },
            "_pivot_id": 2,
            "_pivot_channel_id": 2
        },
        {
            "id": 29,
            "album_id": 2,
            "name": "track29",
            "slug": "track29",
            "track_no": null,
            "track": null,
            "duration": 0,
            "avatar": "track_avatar29.jpg",
            "genre_id": null,
            "genre_id_opt": null,
            "is_explicit": null,
            "created_at": "2015-03-06T14:32:18.000Z",
            "updated_at": "2015-03-06T14:32:18.000Z",
            "album": {
                "id": 2,
                "channel_id": 2,
                "genre_id": null,
                "genre_id_opt": null,
                "name": "alb2",
                "slug": "alb2",
                "description": "aliquid neque autem est dignissimos",
                "release_date": null,
                "avatar": "alb_avatar2.jpg",
                "type": null,
                "is_publish": 1,
                "created_at": "2015-03-06T14:32:18.000Z",
                "updated_at": "2015-03-06T14:32:18.000Z"
            },
            "_pivot_id": 2,
            "_pivot_channel_id": 2
        }
    ]
},
{
    "id": 3,
    "user_id": 6,
    "genre_id": null,
    "genre_id_opt": null,
    "name": "chann2",
    "slug": "chann2",
    "description": "debitis repudiandae oluptatem quod",
    "is_verified": 0,
    "cover": "chann_cover2.jpg",
    "avatar": "chann_avatar2.jpg",
    "created_at": "2015-03-06T14:32:18.000Z",
    "updated_at": "2015-03-06T14:32:18.000Z",
    "publishedTracks": []
},
{
    "id": 4,
    "user_id": 3,
    "genre_id": null,
    "genre_id_opt": null,
    "name": "chann3",
    "slug": "chann3",
    "description": "rerum ut sequuntur dolores",
    "is_verified": 0,
    "cover": "chann_cover3.jpg",
    "avatar": "chann_avatar3.jpg",
    "created_at": "2015-03-06T14:32:18.000Z",
    "updated_at": "2015-03-06T14:32:18.000Z",
    "publishedTracks": []
}

2 个答案:

答案 0 :(得分:1)

你在找这样的东西吗?

//channels is your array that you have put
var obj = [];  
channels.forEach(function(channel) { 
   // it takes publishedTracks of the channel and pushes into a separate array.        
   obj.push(channel.publishedTracks); 

   //remove from channel object
   delete channel.publishedTracks;
});

console.log(obj);

答案 1 :(得分:0)

也许你可以试试

var newArray = JSONObject.map(function (elem) {
    return elem.publishedTracks.reduce(function(prev, current) {
      var result = prev;
      result.push(current);
      return result;
    }, []);
    });