-2

object is not pushing into array.

  groupEventsArray=[]
  groupEvents=(eventOnDate)=>{
    for(i=0;i<eventOnDate.events.length;i++){
      for(j=0;j<eventOnDate.events.length;j++){
        if(eventOnDate.events[i].start==eventOnDate.events[j].start)
        this.groupEventsArray.push(eventsOnDate.events[i])
      }
    }
    console.log(JSON.stringify(this.groupEventsArray))
  }

No error is coming but I think it is stucked in infinite loop. Any help will be appreciated

fake account
  • 91
  • 2
  • 9
  • can you share the `eventOnDate` array? – brk Nov 28 '18 at 09:17
  • you have used the variable as `groupEventsArray=[]`, but while pushing, you are using `this.groupEventsArray` instead of `groupEventsArray` – Panther Nov 28 '18 at 09:19
  • `but I think it is stucked in infinite loop` not unless `eventOnDate.events.length` is infinite ... but you do realise your code will push every `eventOnDate.events` anyway – Jaromanda X Nov 28 '18 at 09:19
  • {"fullDate":"2018-10-26T09:30:00.000Z","events":[{"eventId":"43460","title":"Recap of Day 1 and What to Expect day 2","description":"","start":"1540525500","end":"1540525800","location":"Aura 1&2","type":"normal","limit":0,"remaining":-1,"url":"","networking_event":0,"alreadyRegistered":0,"sharing":1,"qa":1,"calendar":1}, – fake account Nov 28 '18 at 09:21
  • sample: eventOnDetail is like this – fake account Nov 28 '18 at 09:21
  • What wrong in using this if I am using is outside function – fake account Nov 28 '18 at 09:22
  • You are pushing a value from `eventsOnDate`, but your arrow function's parameter is called `eventOnDate` (without the 's'). – djlauk Nov 28 '18 at 09:24
  • ohh yeah ...it's my mistake btw thank you – fake account Nov 28 '18 at 09:28

2 Answers2

1

Consider using forEach() method. The forEach() method calls a provided function once for each element in an array, in order.

Note: forEach() does not execute the function for array elements without values.

Syntax

array.forEach(function(currentValue, index, arr), thisValue)

Example

var numbers = [4, 9, 16, 25];
function myFunction(item, index) {
  console.log(item, index);
}

numbers.forEach(myFunction)

Looping over an array like this help avoid infinte loop.

Ochui Princewill
  • 148
  • 1
  • 15
1

Sounds like a basic group array by key question. You could do it in the following way:

const data = {
  fullDate: '2018-10-26T09:30:00.000Z',
  events: [
    {
      eventId: '43460',
      start: '1540525500',
    },
    {
      eventId: '43461',
      start: '1540525500',
    },
    {
      eventId: '43462',
      start: '1540525500',
    },
    {
      eventId: '43463',
      start: '1540525510',
    },
  ],
};

const createKey = (t) => t.start;
console.log(
  Object.values(
    data.events
      .map((o) => [o, createKey(o)])
      .reduce((result, [item, key]) => {
        result[key] = result[key] || [];
        result[key].push(item);
        return result;
      }, {}),
  ),
);

But the question is probably a duplicate of this one (look for answers not using lodash)

HMR
  • 37,593
  • 24
  • 91
  • 160