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

153
Views
Prevent undefined side effect for new attributes

I have an object like these

object.js

export default [
    {
      id: 1,
      slug: 'oracle-mysql',
      rules: [],
      // has_children: true, // my developer didnt defined this attributes
      // parent_id: 2, // my developer didnt defined this attributes
    },
    {
      id: 2,
      slug: 'hm-gitu',
      rules: ['ada'],
      has_children: true,
      // parent_id: 2, // my developer didnt defined this attributes
    }
    {
      id: 3,
      slug: 'hire',
      rules: ['scodeid'],
      // has_children: true, // my developer didnt defined this attributes
      parent_id: 2, // have
    }

by the way this object has a lot of data, I don't know if there is any defined so I try hard to consume data like these

in here (side-effect) I just set for new attributes for this.form without spread operator.

const { rules } = object.find(f => f.id === this.form.id - 1) // some data static has inject before object.id = 2 , so this.form.id - 1 = 2 ( first data i get rules: ['ada'], } 
try {
   const { has_children, parent_id } = object.find(f => f.id === this.form.id)

    if(parent_id === 2 || parent_id !== undefined) {
     // eslint-disable-next-line vue/no-side-effects-in-computed-properties
     this.form.parent_id = parent_id
     if (has_children) this.form.has_children = true
    }

    this.form.parent_id = parent_id
    this.form.rules = rules

} catch (e) {
   this.form.rules = rules
}

I just want to know how to not use code like these

const { rules } = object.find(f => f.id === this.form.id - 1)
try {
   const { has_children, parent_id } = object.find(f => f.id === this.form.id)

I just want use like these

   const { rules, has_children, parent_id } = object.find(f => f.id === this.form.id)

instead of

const data = object.find(f => f.id === this.form.id)
if(data.has_children) this.form.has_children = data.has_children

any option for set all default if undefined or null or empty [] ? I only know to set default value if empty/null const { rules = [], then how for set attributes undefined

If I know the data its look like on above maybe I'm just going on refactor it but I think it's more efficient about the data, but the code need to re-set for undefined attributes.

about 4 years ago · Juan Pablo Isaza
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!