Skip to main content
added 2174 characters in body
Source Link
const globalLkupRemoveContactReviewer = [
  { objectid: 555, userid: '1', associationid: '1948874' },
  { objectid: 555, userid: '2', associationid: '1950833' },
];

const myStr = '111*1*1968397,222*1*1968398,222*2*1968399,333*2*1968401,333*4*1968402,333*1*19684034';
const data = myStr.split(',')
  .reduce((acc, e) => {
    const [objectid, contactid, associationid] = e.split('*')
    if (!globalLkupRemoveContactReviewer.find((item2) => 
      item2.userid === contactid)) return acc   // skip 

    const parent = acc.find(e => e.objectid === objectid)
    const child = { isdelete: "1", contactid, associationid }
    if (parent) {
      parent.contacts.push(child)     // append contact
    } else {
      acc.push({objectid,contacts: [child]}) // new object
    }
    return acc
  }, [])
console.log(JSON.stringify(data, null, 2))
const globalLkupRemoveContactReviewer = [
  { objectid: 555, userid: '1', associationid: '1948874' },
  { objectid: 555, userid: '2', associationid: '1950833' },
];

const myStr = '111*1*1968397,222*1*1968398,222*2*1968399,333*2*1968401,333*4*1968402,333*1*19684034';
const data = myStr.split(',')
  .reduce((acc, e) => {
    const [objectid, contactid, associationid] = e.split('*')
    if (!globalLkupRemoveContactReviewer.find((item2) => 
      item2.userid === contactid)) return acc   // skip 

    const parent = acc.find(e => e.objectid === objectid)
    const child = { isdelete: "1", contactid, associationid }
    if (parent) {
      parent.contacts.push(child)     // append contact
    } else {
      acc.push({objectid,contacts: [child]}) // new object
    }
    return acc
  }, [])
console.log(JSON.stringify(data, null, 2))

As a result, we do not have unnecessary actions and operations.

UPDATE

Another great example of how to improve performance with reducereduce. The morbusg's answer has a use of SetSet, but you can create it in different ways. I suggest creating it in reduce reduce - we will have one cycleloop. In the case of new Set(x.map(...))new Set(x.map(...)) we have a map loopmap loop and an inner loopinner loop in the Set constructorSet constructor.

const globalLkupRemoveContactReviewer = [
  { objectid: 555, userid: '1', associationid: '1948874' },
  { objectid: 555, userid: '2', associationid: '1950833' },
];

const myStr = '111*1*1968397,222*1*1968398,222*2*1968399,333*2*1968401,333*4*1968402,333*1*19684034';
const data = myStr.split(',')
  .reduce((acc, e) => {
    const [objectid, contactid, associationid] = e.split('*')
    if (!globalLkupRemoveContactReviewer.find((item2) => 
      item2.userid === contactid)) return acc   // skip 

    const parent = acc.find(e => e.objectid === objectid)
    const child = { isdelete: "1", contactid, associationid }
    if (parent) {
      parent.contacts.push(child)     // append contact
    } else {
      acc.push({objectid,contacts: [child]}) // new object
    }
    return acc
  }, [])
console.log(JSON.stringify(data, null, 2))

As a result, we do not have unnecessary actions and operations.

Another great example of how to improve performance with reduce. The morbusg's answer has a use of Set, but you can create it in different ways. I suggest creating it in reduce - we will have one cycle. In the case of new Set(x.map(...)) we have a map loop and an inner loop in the Set constructor.

const globalLkupRemoveContactReviewer = [
  { objectid: 555, userid: '1', associationid: '1948874' },
  { objectid: 555, userid: '2', associationid: '1950833' },
];

const myStr = '111*1*1968397,222*1*1968398,222*2*1968399,333*2*1968401,333*4*1968402,333*1*19684034';
const data = myStr.split(',')
  .reduce((acc, e) => {
    const [objectid, contactid, associationid] = e.split('*')
    if (!globalLkupRemoveContactReviewer.find((item2) => 
      item2.userid === contactid)) return acc   // skip 

    const parent = acc.find(e => e.objectid === objectid)
    const child = { isdelete: "1", contactid, associationid }
    if (parent) {
      parent.contacts.push(child)     // append contact
    } else {
      acc.push({objectid,contacts: [child]}) // new object
    }
    return acc
  }, [])
console.log(JSON.stringify(data, null, 2))

As a result, we do not have unnecessary actions and operations.

UPDATE

Another great example of how to improve performance with reduce. The morbusg's answer has a use of Set, but you can create it in different ways. I suggest creating it in reduce - we will have one loop. In the case of new Set(x.map(...)) we have a map loop and an inner loop in the Set constructor.

added 2174 characters in body
Source Link
const globalLkupRemoveContactReviewer = [
  { objectid: 555, userid: '1', associationid: '1948874' },
  { objectid: 555, userid: '2', associationid: '1950833' },
];

const myStr = '111*1*1968397,222*1*1968398,222*2*1968399,333*2*1968401,333*4*1968402,333*1*19684034';
const data = myStr.split(',')
  .reduce((acc, e) => {
    const [objectid, contactid, associationid] = e.split('*')
    if (!globalLkupRemoveContactReviewer.find((item2) => 
      item2.userid === contactid)) return acc   // skip 

    const parent = acc.find(e => e.objectid === objectid)
    const child = { isdelete: "1", contactid, associationid }
      if (parent) {
      parent.contacts.push(child)     // append contact
    } else {
      acc.push({objectid,contacts: [child]}) // new object
    }
    return acc
  }, [])
console.log(JSON.stringify(data, null, 2))

Another great example of how to improve performance with reduce. The morbusg's answer has a use of Set, but you can create it in different ways. I suggest creating it in reduce - we will have one cycle. In the case of new Set(x.map(...)) we have a map loop and an inner loop in the Set constructor.

const globalLkupRemoveContactReviewer = [
  { objectid: 555, userid: '1', associationid: '1948874' },
  { objectid: 555, userid: '2', associationid: '1950833' },
];
console.time('1')
for (let i=0; i<100; i++) 
  new Set(globalLkupRemoveContactReviewer.map(({userid}) => userid))
console.timeEnd('1')
console.time('2')
for (let i=0; i<100; i++) 
  globalLkupRemoveContactReviewer.reduce((acc, {userid}) => acc.has(userid) ? acc : acc.add(userid), new Set)
console.timeEnd('2')

The final version might look like this:

const globalLkupRemoveContactReviewer = [
  { objectid: 555, userid: '1', associationid: '1948874' },
  { objectid: 555, userid: '2', associationid: '1950833' },
];

const globalRemoveContactId = globalLkupRemoveContactReviewer
  .reduce((acc, {userid}) => acc.has(userid) ? acc : acc.add(userid), new Set)

const myStr = '111*1*1968397,222*1*1968398,222*2*1968399,333*2*1968401,333*4*1968402,333*1*19684034';

const data = myStr.split(',')
  .reduce((acc, e) => {
    const [objectid, contactid, associationid] = e.split('*')
    if (!globalRemoveContactId.has(contactid)) return acc   // skip 

    const parent = acc.find(e => e.objectid === objectid)
    const child = { isdelete: "1", contactid, associationid }
    if (parent) {
      parent.contacts.push(child)     // append contact
    } else {
      acc.push({objectid,contacts: [child]}) // new object
    }
    return acc
  }, [])
console.log(JSON.stringify(data, null, 2))

const globalLkupRemoveContactReviewer = [
  { objectid: 555, userid: '1', associationid: '1948874' },
  { objectid: 555, userid: '2', associationid: '1950833' },
];

const myStr = '111*1*1968397,222*1*1968398,222*2*1968399,333*2*1968401,333*4*1968402,333*1*19684034';
const data = myStr.split(',')
  .reduce((acc, e) => {
    const [objectid, contactid, associationid] = e.split('*')
    if (!globalLkupRemoveContactReviewer.find((item2) => 
      item2.userid === contactid)) return acc   // skip 

    const parent = acc.find(e => e.objectid === objectid)
    const child = { isdelete: "1", contactid, associationid }
      if (parent) {
      parent.contacts.push(child)     // append contact
    } else {
      acc.push({objectid,contacts: [child]}) // new object
    }
    return acc
  }, [])
console.log(JSON.stringify(data, null, 2))
const globalLkupRemoveContactReviewer = [
  { objectid: 555, userid: '1', associationid: '1948874' },
  { objectid: 555, userid: '2', associationid: '1950833' },
];

const myStr = '111*1*1968397,222*1*1968398,222*2*1968399,333*2*1968401,333*4*1968402,333*1*19684034';
const data = myStr.split(',')
  .reduce((acc, e) => {
    const [objectid, contactid, associationid] = e.split('*')
    if (!globalLkupRemoveContactReviewer.find((item2) => 
      item2.userid === contactid)) return acc   // skip 

    const parent = acc.find(e => e.objectid === objectid)
    const child = { isdelete: "1", contactid, associationid }
    if (parent) {
      parent.contacts.push(child)     // append contact
    } else {
      acc.push({objectid,contacts: [child]}) // new object
    }
    return acc
  }, [])
console.log(JSON.stringify(data, null, 2))

Another great example of how to improve performance with reduce. The morbusg's answer has a use of Set, but you can create it in different ways. I suggest creating it in reduce - we will have one cycle. In the case of new Set(x.map(...)) we have a map loop and an inner loop in the Set constructor.

const globalLkupRemoveContactReviewer = [
  { objectid: 555, userid: '1', associationid: '1948874' },
  { objectid: 555, userid: '2', associationid: '1950833' },
];
console.time('1')
for (let i=0; i<100; i++) 
  new Set(globalLkupRemoveContactReviewer.map(({userid}) => userid))
console.timeEnd('1')
console.time('2')
for (let i=0; i<100; i++) 
  globalLkupRemoveContactReviewer.reduce((acc, {userid}) => acc.has(userid) ? acc : acc.add(userid), new Set)
console.timeEnd('2')

The final version might look like this:

const globalLkupRemoveContactReviewer = [
  { objectid: 555, userid: '1', associationid: '1948874' },
  { objectid: 555, userid: '2', associationid: '1950833' },
];

const globalRemoveContactId = globalLkupRemoveContactReviewer
  .reduce((acc, {userid}) => acc.has(userid) ? acc : acc.add(userid), new Set)

const myStr = '111*1*1968397,222*1*1968398,222*2*1968399,333*2*1968401,333*4*1968402,333*1*19684034';

const data = myStr.split(',')
  .reduce((acc, e) => {
    const [objectid, contactid, associationid] = e.split('*')
    if (!globalRemoveContactId.has(contactid)) return acc   // skip 

    const parent = acc.find(e => e.objectid === objectid)
    const child = { isdelete: "1", contactid, associationid }
    if (parent) {
      parent.contacts.push(child)     // append contact
    } else {
      acc.push({objectid,contacts: [child]}) // new object
    }
    return acc
  }, [])
console.log(JSON.stringify(data, null, 2))

added 27 characters in body
Source Link

If I understand everything correctly, then this can be done easier. The code can be formatted in different ways. But the main thing is that there is less action. So:

  • we split the data into parts
  • then through reduce we form a common array of objects
  • parse data for object (including objectid)
  • check globalLkupRemoveContactReviewer if we should add something
  • to save space, we form a child object once
  • if we have an object with this objectid - update it otherwise create it

const globalLkupRemoveContactReviewer = [
  { objectid: 555, userid: '1', associationid: '1948874' },
  { objectid: 555, userid: '2', associationid: '1950833' },
];

const myStr = '111*1*1968397,222*1*1968398,222*2*1968399,333*2*1968401,333*4*1968402,333*1*19684034';
const data = myStr.split(',')
  .reduce((acc, e) => {
    const [objectid, contactid, associationid] = e.split('*')
    if (!globalLkupRemoveContactReviewer.find((item2) => 
      item2.userid === contactid)) return acc   // skip 

    const parent = acc.find(e => e.objectid === objectid)
    const child = { isdelete: "1", contactid, associationid }
    
   if (!globalLkupRemoveContactReviewer.find((item2) => 
    item2.userid === contactid)parent) return acc   // skip {
  
   if (parent) parent.contacts.push(child)     // append contact
    } else {
      acc.push({objectid,contacts: [child]}) // new object
    }
    return acc
  }, [])
console.log(JSON.stringify(data, null, 2))

As a result, we do not have unnecessary actions and operations.

If I understand everything correctly, then this can be done easier. The code can be formatted in different ways. But the main thing is that there is less action. So:

  • we split the data into parts
  • then through reduce we form a common array of objects
  • parse data for object (including objectid)
  • check globalLkupRemoveContactReviewer if we should add something
  • to save space, we form a child object once
  • if we have an object with this objectid - update it otherwise create it

const globalLkupRemoveContactReviewer = [
  { objectid: 555, userid: '1', associationid: '1948874' },
  { objectid: 555, userid: '2', associationid: '1950833' },
];

const myStr = '111*1*1968397,222*1*1968398,222*2*1968399,333*2*1968401,333*4*1968402,333*1*19684034';
const data = myStr.split(',')
  .reduce((acc, e) => {
    const [objectid, contactid, associationid] = e.split('*')
    const parent = acc.find(e => e.objectid === objectid)
    const child = { isdelete: "1", contactid, associationid }
    
   if (!globalLkupRemoveContactReviewer.find((item2) => 
    item2.userid === contactid)) return acc   // skip 
  
   if (parent) parent.contacts.push(child)     // append contact
  else acc.push({objectid,contacts: [child]}) // new object
  
  return acc
  }, [])
console.log(JSON.stringify(data, null, 2))

As a result, we do not have unnecessary actions and operations.

If I understand everything correctly, then this can be done easier. The code can be formatted in different ways. But the main thing is that there is less action. So:

  • we split the data into parts
  • then through reduce we form a common array of objects
  • parse data for object (including objectid)
  • check globalLkupRemoveContactReviewer if we should add something
  • to save space, we form a child object once
  • if we have an object with this objectid - update it otherwise create it

const globalLkupRemoveContactReviewer = [
  { objectid: 555, userid: '1', associationid: '1948874' },
  { objectid: 555, userid: '2', associationid: '1950833' },
];

const myStr = '111*1*1968397,222*1*1968398,222*2*1968399,333*2*1968401,333*4*1968402,333*1*19684034';
const data = myStr.split(',')
  .reduce((acc, e) => {
    const [objectid, contactid, associationid] = e.split('*')
    if (!globalLkupRemoveContactReviewer.find((item2) => 
      item2.userid === contactid)) return acc   // skip 

    const parent = acc.find(e => e.objectid === objectid)
    const child = { isdelete: "1", contactid, associationid }
      if (parent) {
      parent.contacts.push(child)     // append contact
    } else {
      acc.push({objectid,contacts: [child]}) // new object
    }
    return acc
  }, [])
console.log(JSON.stringify(data, null, 2))

As a result, we do not have unnecessary actions and operations.

added 47 characters in body
Source Link
Loading
added 1 character in body
Source Link
Loading
deleted 3 characters in body
Source Link
Loading
deleted 1 character in body
Source Link
Loading
added 315 characters in body
Source Link
Loading
added 315 characters in body
Source Link
Loading
Source Link
Loading