0

I got an array like this and I need to filter this array with the same user id with all the occurrences with the service_id corresponding to user_id:

Array [
  Object {
    "service_id": 14,
    "user_id": 56,
  },
  Object {
    "service_id": 19,
    "user_id": 59,
  },
  Object {
    "service_id": 18,
    "user_id": 56,
  },
  Object {
    "service_id": 18,
    "user_id": 56,
  },
]

And I need to filter the array like this:

Array [
  Object {
    "user_id": 56,
    [
      {"service_id":14},
      {"service_id": 18}
    ]
  },
  Object {
    "user_id": 59,
    [
      {"service_id": 19},
    ]
  },
]
Edinho Rodrigues
  • 354
  • 4
  • 24
  • 1
    FYI: What you want to do can be better described as grouping. You want to _group_ your array of objects by `user_id`. – Terry Aug 05 '21 at 13:31
  • this is easily solvable with a loop, and a hashmap since all javascript objects are hashmaps. what have you tried so far? – Tom Elias Aug 05 '21 at 13:31
  • 2
    You can look [here](https://stackoverflow.com/questions/40774697/how-to-group-an-array-of-objects-by-key) – Nikola Pavicevic Aug 05 '21 at 13:37
  • 1
    Actually, your expected result is not valid as there is no key for the children array. – ikhvjs Aug 05 '21 at 14:03

2 Answers2

2

var array = [{"service_id": 14,"user_id": 56,},{"service_id": 19,"user_id": 59,},
             {"service_id": 18,"user_id": 56,},{"service_id": 18,"user_id": 56}]

const groupByUserId = (array, key) => {
  return array.reduce((result, currentValue) => {(
  //Create a new array as key if there is not found
  result[currentValue[key]] = result[currentValue[key]] || []).push(currentValue);
  return result;
  }, {}); // empty object after initialization
};

const grouped = groupByUserId(array, 'user_id');
console.log(grouped)

In Javascript your array should look like this:

var array = [{"service_id": 14,"user_id": 56},{"service_id": 19,"user_id": 59},
             {"service_id": 18,"user_id": 56},{"service_id": 18,"user_id": 56}]

There is an assumption to be made here to identify user_id as key in the new grouped array in order to populate the user's similar service_id inside its corresponding array.

const groupByUserId = (array, key) => {
    return array.reduce((result, currentValue) => {(

    //Create a new array as key if there is not found
    result[currentValue[key]] = result[currentValue[key]] || []).push(currentValue);
    return result;
      }, {}); // empty object after initialization
    };
    
    const grouped = groupByUserId(array, 'user_id');
    console.log(grouped)
George Gotsidis
  • 426
  • 4
  • 15
  • 2
    Hi, you can also use SO code snippet. https://meta.stackoverflow.com/questions/356678/stack-overflow-run-code-snippet – ikhvjs Aug 13 '21 at 07:30
1

Your output array must be as the below example I suppose.

You can use Array.reduce for that.

const data = [
    {
        "service_id": 14,
        "user_id": 56,
    },
    {
        "service_id": 19,
        "user_id": 59,
    },
    {
        "service_id": 18,
        "user_id": 56,
    },
    {
        "service_id": 18,
        "user_id": 56,
    },
]
const output = data.reduce((acc, curr) => {
    const node = acc.find((item) => item.user_id === curr.user_id);
    if (node) {
        node.list.push({service_id: curr.service_id})
    } else {
        acc.push({
            user_id: curr.user_id,
            list: [{service_id: curr.service_id}]
        })
    }
    return acc;
}, []);
console.log(output)
Nitheesh
  • 19,238
  • 3
  • 22
  • 49