0

I have javascript that is checking multiple fields in Dynamics CRM. If some of them or all are "1" it will use the highest value to use in a calculation. I pasted a part of the script:

var level1 = {
percentage: 0, 
label: "MyLabel"
}

var level2 = {
percentage: 0, 
label: "MySecondLabel"
}

if (Xrm.Page.getAttribute("conditioninterested").getValue() == 1) {
    level1[0] = 50;
}

if (Xrm.Page.getAttribute("revenueconditionquote").getValue() == 1) {
level2[0] = 70;
}

var chance = Math.max(level1[0],level2[0]);

How do I get the corresponding label after the Math.max found out the percentage to use?

2 Answers 2

1

With the way your data is structured I would add the level objects to an array and then filter by chance. filter returns an array so you need to take the first element [0] which is an object, and then the label value from that object.

var label = [level1, level2].filter(function (el) {
  return el[0] === chance;
})[0].label; // MySecondLabel

DEMO

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

1 Comment

Thanks a lot that helped!
0

First of all, you are could have made a constructor function to declare the object instances, secondly level1 and level2 are objects, So basically there are two ways you can access object properties:

  • Dot notation, eg. level1.lebel or level1.percentage
  • the [] notation. You need to put the key in, not the index number. Objects don't have indexes, they are key and value pairs. and also they are un-ordered.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.