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.