Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

221
Views
Remove all array of objects that contain same key and has at least one value true

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
  }
]

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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)

over 4 years ago · Santiago Trujillo Report

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))

over 4 years ago · Santiago Trujillo Report

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)

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!