0
\$\begingroup\$

I am required to create a bunch of object nodes to an object:

this.selectedText = e.text;
this.clipNo = e.clipNo;
if(!this.clipCollection.hasOwnProperty(this.category)) {
 this.clipCollection[this.category] = {};
}
if(!this.clipCollection.hasOwnProperty("grandClipCount")) {
 this.clipCollection.grandClipCount = 0;
}
if(!this.clipCollection[this.category].hasOwnProperty(['pageNo'+this.pageNumber])) {
 this.clipCollection[this.category]['pageNo'+this.pageNumber] = {};
 this.clipCollection[this.category]['pageNo'+this.pageNumber].pageServerInfo = this.pageServerInfo;
}
if(!this.clipCollection[this.category][this.category+"ClipCount"]) {
 this.clipCollection[this.category][this.category+"ClipCount"] = 0;
}
if(!this.clipCollection[this.category]['pageNo'+this.pageNumber].hasOwnProperty("clips")) {
 this.clipCollection[this.category]['pageNo'+this.pageNumber].clips = {};
 this.clipCollection[this.category]['pageNo'+this.pageNumber].clips['clip'+this.clipNo] = this.selectedText;
 this.clipCollection[this.category]['pageNo'+this.pageNumber].pageClipCount = 0;
}
this.clipCollection[this.category]['pageNo'+this.pageNumber].clips['clip'+this.clipNo] = this.selectedText;
this.clipCollection[this.category]['pageNo'+this.pageNumber].pageClipCount += 1;
this.clipCollection[this.category][this.category+"ClipCount"] += 1;
this.clipCollection.grandClipCount += 1;

Is there a short approach to minimize this code?

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Feb 7, 2015 at 3:28
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Well for starters maybe get rid of this with a few local variables and give names to often used constants, e.g.

var pageNo = 'pageNo'+this.pageNumber;

or so.

The blocks with hasOwnProperty checks can be replaced with a conditional:

this.clipCollection[this.category] = this.clipCollection[this.category] || {};
this.clipCollection.grandClipCount = (this.clipCollection.grandClipCount || 0) + 1;

And so on.

answered Feb 7, 2015 at 14:41
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.