6

I am using Vue and suddenly some of the computed css using vuetify is not working.

The way I declare an object is

personal_info : {}

and in my template, I could just do personal_info.name and other more in every v-model of text input.

I have no errors but suddenly the vuetify has a class called input-group--dirty that will elevate the label of the text input whenever it's not empty. But suddenly, it's not working. It looks like this:

enter image description here

As you can see, the text and label are overlapping.
The only thing that make it work is to set the property to null which is:

personal_info : {
  name: null
}

The problem is that I have hundreds of text inputs and I dont want to set everything to null.

Is there a simple way to set all of the object's properties to null instead of coding it 1 by 1?

2
  • You can just set: personal_info = {} Commented Feb 22, 2017 at 7:24
  • @Saurabh Updated my post Commented Feb 22, 2017 at 7:27

3 Answers 3

10

checkout this snippet

var personal_info = {
  name: 'john',
  email: '[email protected]',
  phone: 9876543210
}
console.log(JSON.stringify(personal_info)); //before looping

for (var key in personal_info ) {
  personal_info[key] = null;
}

console.log(JSON.stringify(personal_info));//after looping and setting value to 'null'

Sign up to request clarification or add additional context in comments.

8 Comments

Combining that with my code it won't work because we know javascript is synchronous
@FewFlyBy what problem you are facing now? how this object is generated? are you using any/more api calls to generate it?
I'm not using any api etc.. I still get the same thing
Okay, Would you mind to share what kinf of data is stored in personal_info? OR You can create a fiddle and paste link
You interested if I put the project in github? Just npm install and npm run dev it after..
|
2

Vilas example is ok. But in case you have nested properties and your obj looks like this you could try my snippet

var obj = {
  a: 1 ,
  b: 2,
  c: {
    e:3,
    b: {
      d:6,
      e: ['23']
    }
  }
};

var setProps = function(flat, newVal){
   for(var i in flat){
     if((typeof flat[i] === "object") && !(flat[i] instanceof Array)){
       setProps(flat[i], newVal);
       return;
     } else {
       flat[i] = newVal;
     }
  }
}
setProps(obj, null);

console.log(JSON.stringify(obj));

Comments

0

you can use simple immutable oneliner:

Object.fromEntries(Object.entries(YOUR_OBJECT).map(([key]) => [key, null])))

const bio = {
  name: 'john',
  age: 22,
  hobbies: ['soccer']
}
const resetBio = Object.fromEntries(Object.entries(bio).map(([key]) => [key, null]))

console.log(bio)
console.log(resetBio)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.