我如何迭代此数组?

时间:2019-06-14 09:15:55

标签: jquery arrays ecmascript-5

我在一个数组中需要一个数据。

我必须使每个索引内的events数组成为一个以events数组为索引的数组。

这是我的尝试:

//array

    var events = {
    "events": [{
        "id": 3,
        "user_id": "48f8fd57-5116-419c-b43a-cff90a4ae39b",
        "event_id": 102,
        "created_at": "2019-06-13 20:46:40",
        "updated_at": "2019-06-13 20:46:40",
        "events": [{
            "id": 102,
            "title": "asdsadsa",
            "description": "dsadsadsa",
            "url": "asdsad",
            "color": "#000000",
            "start": "2019-06-04 08:30:00",
            "end": "2019-06-04 13:00:00"
        }]
    }, {
        "id": 9,
        "user_id": "48f8fd57-5116-419c-b43a-cff90a4ae39b",
        "event_id": 108,
        "created_at": "2019-06-14 10:41:13",
        "updated_at": "2019-06-14 10:41:13",
        "events": [{
            "id": 108,
            "title": "GC",
            "description": "FDGDFGDFG",
            "url": "DFGFDG",
            "color": "#000000",
            "start": "2019-06-25 22:00:00",
            "end": "2019-06-29 22:00:00"
        }]
    }]
}

    // events = this array
    var parseEvents = function(events) {
            var eventArray = [];

            jQuery.each(events, function(index, item) {
                eventArray.push(item['events']);
            });

            return (eventArray);
        }

     console.log(parseEvents(events));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

结果应为:

{ 1 //index : [{
        "id": 102,
        "title": "asdsadsa",
        "description": "dsadsadsa",
        "url": "asdsad",
        "color": "#000000",
        "start": "2019-06-04 08:30:00",
        "end": "2019-06-04 13:00:00"
    }],
2: [{
        "id": 102,
        "title": "asdsadsa",
        "description": "dsadsadsa",
        "url": "asdsad",
        "color": "#000000",
        "start": "2019-06-04 08:30:00",
        "end": "2019-06-04 13:00:00"
    }],
}

类似这样,我希望事件数组中的每个事件数组都成为索引

如何从每个索引中获取数组事件并推送到数组。

3 个答案:

答案 0 :(得分:3)

假设您想要所有事件的单个数组,则没有理由为此使用jQuery,而仅是JavaScript标准库。

在ES5中:

var parseEvents = function(events) {
    var eventArray = [];
    events.events.forEach(function(item) {
        eventArray.push.apply(eventArray, item.events);
    });
    return eventArray;
};

您的events对象具有events属性,该属性是具有events属性的对象的数组,因此上述内容循环遍历这些对象并将其事件推送到{{1} }。这行:

eventArray

有效调用eventArray.push.apply(eventArray, item.events); 的次数与eventArray.push(event, event, event, event)中发生的事件一样多。有关详细信息,请参见apply on MDN

实时示例:

item.events

在ES2015 +中更清晰:

var events = {
  "events": [{
    "id": 3,
    "user_id": "48f8fd57-5116-419c-b43a-cff90a4ae39b",
    "event_id": 102,
    "created_at": "2019-06-13 20:46:40",
    "updated_at": "2019-06-13 20:46:40",
    "events": [{
      "id": 102,
      "title": "asdsadsa",
      "description": "dsadsadsa",
      "url": "asdsad",
      "color": "#000000",
      "start": "2019-06-04 08:30:00",
      "end": "2019-06-04 13:00:00"
    }]
  }, {
    "id": 4,
    "user_id": "48f8fd57-5116-419c-b43a-cff90a4ae39b",
    "event_id": 103,
    "created_at": "2019-06-13 20:46:45",
    "updated_at": "2019-06-13 20:46:45",
    "events": [{
      "id": 103,
      "title": "asdsad",
      "description": "sadsad",
      "url": "",
      "color": "#000000",
      "start": "2019-06-04 08:30:00",
      "end": "2019-06-04 13:00:00"
    }]
  }]
}

var parseEvents = function(events) {
    var eventArray = [];

    events.events.forEach(function(item) {
        eventArray.push.apply(eventArray, item.events);
    });
    return eventArray;
};

console.log(parseEvents(events));

使用const parseEvents = function(events) { const eventArray = []; for (const item of events.events) { eventArray.push(...item.events); } return eventArray; }; 遍历for-of并扩展表示法,将events.events中的所有条目推入item.events

实时示例:

eventArray

答案 1 :(得分:1)

简单的解决方案:我希望这会有所帮助

var parseEvents = function(events) {
 var eventArray = [];

 jQuery.each(events.events, function(index, item) {
    eventArray.push(item['events']);
 });

return (eventArray);
}

答案 2 :(得分:0)

    let events =   {
        "events": [{
            "id": 3,
            "user_id": "48f8fd57-5116-419c-b43a-cff90a4ae39b",
            "event_id": 102,
            "created_at": "2019-06-13 20:46:40",
            "updated_at": "2019-06-13 20:46:40",
            "events": [{
                "id": 102,
                "title": "asdsadsa",
                "description": "dsadsadsa",
                "url": "asdsad",
                "color": "#000000",
                "start": "2019-06-04 08:30:00",
                "end": "2019-06-04 13:00:00"
            }]
        }, {
            "id": 9,
            "user_id": "48f8fd57-5116-419c-b43a-cff90a4ae39b",
            "event_id": 108,
            "created_at": "2019-06-14 10:41:13",
            "updated_at": "2019-06-14 10:41:13",
            "events": [{
                "id": 108,
                "title": "GC",
                "description": "FDGDFGDFG",
                "url": "DFGFDG",
                "color": "#000000",
                "start": "2019-06-25 22:00:00",
                "end": "2019-06-29 22:00:00"
            }]
        }]
    } 

    

// events = this array
    var parseEvents = function(events) {
      var obj = {}
      events.events.forEach(function(item,indx){
          Object.assign(obj, {[indx+1]: item.events})               
      })
        return (obj);
    }

 console.log(parseEvents(events));