skip to Main Content

I have an Array of objects and I need to return this collection like Object and they key-names need to be indexes of length. I need to filter this object for it values.

Here is my code:

const data = [
  { index: 1, value: "111" },
  { index: 2, value: "121" },
  { index: 3, value: "111" },
  { index: 5, value: "111" },
  { index: 6, value: "121" },
  { index: 7, value: "121" },
];

const getGroupBy = (data) => {
  return data.reduce((acc, curr, currIndex, arr) => {
    const val = curr.value;
    const idx = curr.index;
    const fValues = arr.filter((el) => el.value === val).map(el => el.index);
    if (acc.hasOwnProperty(currIndex)) {
      acc[currIndex] = arr.filter((el) => el.value === val);
    } else {
      Object.assign(acc, { [0]: [idx] });
    }
    return acc;
  }, {});
};

console.log(getGroupBy(data));

My expected output is :

{
    0: [1,3,5],
    1: [2,6,7]
}

4

Answers


  1. Is this not more useful, to key the arrays by their values instead of 0,1?

    Anyway here is how to reduce and how to get either 0,1 keyed or keyed on value

    const data = [
      { index: 1, value: "111" },
      { index: 2, value: "121" },
      { index: 3, value: "111" },
      { index: 5, value: "111" },
      { index: 6, value: "121" },
      { index: 7, value: "121" },
    ];
    
    const indicii = data.reduce((acc,{index,value}) => {
      (acc[value] ??= []).push(index);
      return acc;
    },{})
    console.log({...Object.values(indicii)})
    // or 
    console.log(indicii);

    Interesting method suggested here

    const data = [
      { index: 1, value: "111" },
      { index: 2, value: "121" },
      { index: 3, value: "111" },
      { index: 5, value: "111" },
      { index: 6, value: "121" },
      { index: 7, value: "121" },
    ];
    
    const indicii = data
      .reduce((acc,{index,value},i, arr) => {
        (acc[value] ??= []).push(index);
        return i < arr.length ? acc : {...Object.values(acc) }; // return the requested format at end
      },{});
    
    console.log(indicii);
    Login or Signup to reply.
  2. You could group and assign the values to an object.

    const
        data = [{ index: 1, value: "111" }, { index: 2, value: "121" }, { index: 3, value: "111" }, { index: 5, value: "111" }, { index: 6, value: "121" }, { index: 7, value: "121" }],
        result = Object.assign(
            {},
            Object.values(data.reduce((r, { index, value }) => ((r[value] ??= []).push(index), r), {}))
        );
    
    console.log(result);
    .as-console-wrapper { max-height: 100% !important; top: 0; }
    Login or Signup to reply.
  3. I think you have to go through the step

    1. {value: [index]}
    2. {0: value}

    this step is help your code more cleaner

      const data = [
        { index: 1, value: '111' },
        { index: 2, value: '121' },
        { index: 3, value: '111' },
        { index: 5, value: '111' },
        { index: 6, value: '121' },
        { index: 7, value: '121' },
      ];
    
      const getGroupBy = (data) => {
        return data.reduce((acc, curr) => {
          if (acc[curr.value]) acc[curr.value].push(curr.index);
          else acc[curr.value] = [curr.index];
          return acc;
        }, {});
      };
      console.log(getGroupBy(data));
      console.log({ ...Object.values(getGroupBy(data)) });
    Login or Signup to reply.
  4. The following code, I used reduce but last iteration, I changed the output to the desired one.

    This is useful manytimes to get the desired output. when index is for last item, we are done with all the elements.

    const data = [
      { index: 1, value: "111" },
      { index: 2, value: "121" },
      { index: 3, value: "111" },
      { index: 5, value: "111" },
      { index: 6, value: "121" },
      { index: 7, value: "121" },
    ];
    
    const getGroupBy = (data) => {
      return data.reduce((acc, curr, index) => {
        const val = curr.value;
        const idx = curr.index;
        var key  = val;
         
        if (!acc[key]) {
          acc[key] = [];
        }
        acc[key].push(idx);
    
         if (index == data.length-1) {
          return Object.values(acc).reduce((a,c, i)=> { a[i]=c; return a;}, {});
        }
        return acc;
      }, {});
    };
    
    
    console.log(getGroupBy(data))
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search