Home > OS >  Remove all array of objects that contain same key and has at least one value true
Remove all array of objects that contain same key and has at least one value true

Time:10-08

Hi I have an array of objects which string starts with a specific prefix and if that key value is true then remove all the objects in an array that contains the same key(prefix)

below is the array of object:

const data = [{
    field_name_key: "Recive_IsViaEmail",
    fieldValue: false
  },
  {
    field_name_key: "Recive_IsViaSMS",
    fieldValue: false
  },
  {
    field_name_key: "Sender_IsViaEmail",
    fieldValue: false
  },
  {
    field_name_key: "Sender_IsViaSMS",
    fieldValue: true
  },

]

here "Sender_IsViaSMS" contains true hence remove all the objects that start with the prefix key Sender_IsVia

Final result is this:

const data = [{
    field_name_key: "Recive_IsViaEmail",
    fieldValue: false
  },
  {
    field_name_key: "Recive_IsViaSMS",
    fieldValue: false
  }
]

CodePudding user response:

An inefficient but short solution would be to use Array.filter and set the condition of the callback to whether data contains an item with the same field_name_key property prefix and whose fieldValue property is true:

const data=[{field_name_key:"Recive_IsViaEmail",fieldValue:false},{field_name_key:"Recive_IsViaSMS",fieldValue:false},{field_name_key:"Sender_IsViaEmail",fieldValue:false},{field_name_key:"Sender_IsViaSMS",fieldValue:true}];

const res = data.filter(e => !data.find(f => f.field_name_key.split("IsVia")[0] == e.field_name_key.split("IsVia")[0] && f.fieldValue))

console.log(res)

CodePudding user response:

Build up a list of the prefixes to remove first, then just run a filter.

I am not sure on how you are coming up with the prefix of a given field name however. Given your example you're expecting "Sender_IsViaSMS" to also purge "Recive_IsViaSMS". But you should be able to take this and adapt it as you see fit.

const data=[{field_name_key:"Recive_IsViaEmail",fieldValue:false},{field_name_key:"Recive_IsViaSMS",fieldValue:false},{field_name_key:"Sender_IsViaEmail",fieldValue:false},{field_name_key:"Sender_IsViaSMS",fieldValue:true}];

const prefixsToAxe = new Set();

// Having trouble with this part in your question, so ran with this, replace as you see fit
const determinePrefix = (field_name_key) => field_name_key.split('IsVia')[0];

for (const entryToAxe of data.filter(x => x.fieldValue === true)) {
  const prefixToAxe = determinePrefix(entryToAxe.field_name_key);
  prefixsToAxe.add(prefixToAxe);
}

const purged = data.filter(entry => {
  const prefixInQuestion = determinePrefix(entry.field_name_key);
  return !prefixsToAxe.has(prefixInQuestion);
})

console.log(purged)

CodePudding user response:

You can create a function that first finds the entries to remove, then remove one by one:

const data = [{
    field_name_key: "Recive_IsViaEmail",
    fieldValue: false
  },
  {
    field_name_key: "Recive_IsViaSMS",
    fieldValue: false
  },
  {
    field_name_key: "Sender_IsViaEmail",
    fieldValue: false
  },
  {
    field_name_key: "Sender_IsViaSMS",
    fieldValue: true
  },

]

const sanitizeData = (list) => {
    // find entry keys to remove
    const toRemove = list.reduce((prev, el) => {
        if(el.fieldValue === true) return el.field_name_key.split('_')[0]
    })
    // remove
    const removed = list.filter(el => {
        return !toRemove.includes(el.field_name_key.split('_')[0])
    })
    return removed
}

console.log(sanitizeData(data))

  • Related