Home > Back-end >  Javascript: object map reduce logic not working
Javascript: object map reduce logic not working

Time:12-02

I have the following data structure and script, I am trying to list the recipient's subscriptions in two formats, one showing the subscriptions under services key with true or false (done - working) and the other one servicesDEV im currently struggling with, i want the subscribed ids listed i.e subs:[3218143,8956799] Here are the history of my painful tries https://jsfiddle.net/aectk8v1/8/

let recipients = [
{"id":"666777","lawfulBasis":"0","jurisdiction":"AMER","name":"David G"},
{"id":"888999","lawfulBasis":"1","jurisdiction":"ASIA","name":"Mike A"},
{"id":"444555","lawfulBasis":"2","jurisdiction":"EUR","name":"John No Sub"}
];

let serviceSubscriptions =  [
{"recipientId":"666777","serviceId":"3218143"},
{"recipientId":"666777","serviceId":"8956799"},
{"recipientId":"888999","serviceId":"15656200"},
{"recipientId":"000000","serviceId":"3721"}
];


const recipientObj = recipients.map(({id,...recipient}) => {

      return {
            id,
          isSubscribed: serviceSubscriptions.some(( subs ) => subs.recipientId ===id),          

           /* my data structure for services key is concatenated i think is wrong
example: ["3218143:true", "8956799:true", "15656200:false", "3721:false"],
*/
          services: serviceSubscriptions.map(({recipientId,serviceId}) => serviceId ':' serviceSubscriptions.some(({serviceId}) => recipientId === id)),
          
          /*not working logic here HELP!!!!!!!********/
          servicesDEV:[serviceSubscriptions.map((x) => x).reduce((acc, { recipientId, serviceId }) =>
      acc.set(recipientId,
        (acc.get(recipientId) ?? new Set).add(serviceId)), new Map)
    .entries()].map((serviceIds) => serviceIds.serviceId),
          /*****************/
          
          ...recipient,
          
        }

});

console.log(recipientObj)

Expected outcome of servicesDev key

  id: "666777",
  isSubscribed: true,
  jurisdiction: "AMER",
  lawfulBasis: "0",
  name: "David G",
  services: ["3218143:true", "8956799:true", "15656200:false", "3721:false"],
  servicesDEV: [3218143,8956799]

CodePudding user response:

If I understood the question correctly, here is an attempt

let recipients = [
{"id":"666777","lawfulBasis":"0","jurisdiction":"AMER","name":"David G"},
{"id":"888999","lawfulBasis":"1","jurisdiction":"ASIA","name":"Mike A"},
{"id":"444555","lawfulBasis":"2","jurisdiction":"EUR","name":"John No Sub"}
];

let serviceSubscriptions =  [
{"recipientId":"666777","serviceId":"3218143"},
{"recipientId":"666777","serviceId":"8956799"},
{"recipientId":"888999","serviceId":"15656200"},
{"recipientId":"000000","serviceId":"3721"}
];

const result = recipients.map(recipient => {
  // Create an array of the recipient's subscription IDs
  const subscriptionIds = serviceSubscriptions
    .filter(subscription => subscription.recipientId === recipient.id)
    .map(subscription => subscription.serviceId);

  return {
    ...recipient,
    subscriptions: {
      services: subscriptionIds,
      servicesDEV: [`subs:${JSON.stringify(subscriptionIds)}`]
    }
  };
});

console.log(result);

EDIT check the snippet below to have services as the main key to the object https://jsfiddle.net/a0d31b2o/

Edited answer

// Use the reduce() method to create the recipientServices object that maps each recipient ID to an array of their service subscriptions
let recipientServices = serviceSubscriptions.reduce((services, subscription) => {
  // If the recipient has not been added to the services object yet, create an entry for them
  if (!services[subscription.recipientId]) {
    services[subscription.recipientId] = [];
  }

  // Add the service to the array of services for the current recipient
  services[subscription.recipientId].push(subscription.serviceId);

  return services;
}, {});

// Use the reduce() method to create the recipient objects and add them to an array to store the result
let resultArray = recipients.reduce((result, recipient) => {
  // Create a new object for the current recipient
  let recipientObject = {
    id: recipient.id,
    isSubscribed: false,
    jurisdiction: recipient.jurisdiction,
    lawfulBasis: recipient.lawfulBasis,
    name: recipient.name,
    services: [],
    servicesDEV: []
  };

  // If the recipient is in the recipientServices object, set isSubscribed to true and add their services to the services array
  if (recipientServices[recipient.id]) {
    recipientObject.isSubscribed = true;
    recipientObject.services = recipientServices[recipient.id].map(serviceId => serviceId   ":true");
    recipientObject.servicesDEV = recipientServices[recipient.id];
  }

  // Add the new object to the result array
  result.push(recipientObject);

  return result;
}, []);

console.log(resultArray)
  • Related