1

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
  }
]
vinuta
  • 423
  • 9
  • 29
  • i think you will find [this Q&A](https://stackoverflow.com/a/67142812/633183) to be exactly what you're looking for – Mulan Oct 08 '21 at 04:02

3 Answers3

2

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)
Spectric
  • 30,714
  • 6
  • 20
  • 43
0

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))
ABDULLOKH MUKHAMMADJONOV
  • 4,249
  • 3
  • 22
  • 40
  • { "field_name_key": "Sender_IsViaEmail", "fieldValue": false } This should not be in the final result because "Sender_IsViaSMS" is true hence the key which has prefix Sender_IsVia all object which contains same prefix should be removed – vinuta Oct 08 '21 at 04:40
  • Ok. I improved the answer. Chek it out – ABDULLOKH MUKHAMMADJONOV Oct 08 '21 at 05:03
0

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)
AlienWithPizza
  • 386
  • 1
  • 8