0

I am trying to loop through array of entries using a for loop and use the reduce method, but the reduce returns after the first entry, never getting to the second, third, etc entries to push the result to an array v.

Any thoughts?

export const groupEventData = eventData => {
    console.log(eventData)
    // returns[{
    //     "id": "141",
    //     "name": "00333",
    //     "no_transmission": true,
    //     "low_battery" :true,
    // },
    // {
    //     "id": "307",
    //     "name": "01163",
    //     "no_transmission": true,
    //     "low_data_rate": true,
    // }
    // ]

    // now let's group similar event types
    let v = [];
    let types = ["no_transmission", "low_data_rate", "low_battery"]; 
    for (let x = 0; x <= types.length; x++) {
        let data = eventData.reduce((acc, i) => {
            if (!acc[i[types[x]]] && typeof i[types[x]] !== 'undefined') {
                acc[i[types[x]]] = [i]
            }
            else if (Array.isArray(acc[i[types[x]]])) {
                acc[i[types[x]]].push(i);
            }
            else if (typeof acc[i[types[x]]] === 'object') {
                acc[i[types[x]]] = [acc[i[types[x]]]]
                acc[i[types[x]]].push(i)
            }
            return acc;
        }, {});
        // Doesn't add a property for the type if there are no data
        Object.keys(data).length && v.push({ [types[x]: data });
        return v;
    };

}
12
  • 2
    Can you show your input as a minimal reproducible example? This leaves a bit too much to the imagination (type, types, eventData, actual/expected output). Thanks. Commented Sep 29, 2020 at 17:53
  • More importantly, getEventTypes() - is this possible a sync? Commented Sep 29, 2020 at 17:54
  • thank you, clarified Commented Sep 29, 2020 at 18:00
  • Thanks but it's still needlessly abstract. It should be easy to put this into a form with clearly hardcoded input/output data structures that can be executed as a runnable snippet. What output are you trying to achieve here (show it explicitly/literally)? Also, this seems agnostic of React, so I'd encourage removing the tag if it doesn't matter. Thanks. Commented Sep 29, 2020 at 18:18
  • I don't think so, all I want to do is to be able to loop the reduce function the number of times as the length of the types array (in the case above, it's 3 times) but it returns after done the first entry.. makes sense because there is return acc. What I am asking how to loop the reduce function n number of times and store in v array Commented Sep 29, 2020 at 18:33

1 Answer 1

1

You have return v inside your for loop. Try bumping it below the curly brace immediately beneath it. Right now the for loop is iterating only once and then returning a value.

Worked for me below:

const eventData = [
  {"id": "141","name": "00333","no_transmission": true,"low_battery": true},
  {"id": "307","name": "01163","no_transmission": true,"low_data_rate": true}
]

const type = 'bc'

const types = ["no_transmission", "low_data_rate", "low_battery"]

const groupEventData = (eventData, type) => {
  let v = [];
  for (let x = 0; x <= types.length; x++) {
    let data = eventData.reduce((acc, i) => {
      if (!acc[i[types[x]]] && typeof i[types[x]] !== 'undefined') {
        acc[i[types[x]]] = [i]
      }
      else if (Array.isArray(acc[i[types[x]]])) {
        acc[i[types[x]]].push(i);
      }
      else if (typeof acc[i[types[x]]] === 'object') {
        acc[i[types[x]]] = [acc[i[types[x]]]]
        acc[i[types[x]]].push(i)
      }
      return acc;
    }, {});
    Object.keys(data).length && v.push({ [`${types[x]}_${type}`]: data });
  };
  return v;
}

console.log('groupEventData', groupEventData(eventData, type))
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.