Boomerang20thCentury
Boomerang20thCentury

Reputation: 148

Facing problem in retrieving a specific key name from a JSON api

I have an API with the follwing JSON signature:

[{
   "id": 3,
   "impact": 7,
   "probability": 0.7,
   "title": "Risk 3"
}]

Now I have lets say 3 similar objects in this format which actually demonstrates different risk names or numbers of a company. Now I need to find which risk number has the highest impact or probability. Also which risk has the highest RPZ,

 RPZ = impact * probability 

I have managed to find the highest values of any of the properties in the json. But I have tried lot of ways to figure out the title or risk name associated to it. This is my target which I am not able to do. For example my goal is to get the highest value, lets say the impact from the above example, which is 7 and also to print out its risk name which is the title here. I was able to get the highest values but failing to print out its corresponding risk name or title.

a liitle snippet....

var con = JSON.parse(body);

var highImpact = Math.max.apply(Math, con.map(i => i.impact));

now from the above highImpact which is 7, now I need to pull out the corresponding risk name which would be......Risk 3. This is where I am struggling.

I have checked a lot of similar questions asked by other developers in this community and also several web pages and blogs found in the internet. But nothing got me a solution till now.

If some one of you can help me it would be really grateful

v

ar Request = require('request');


Request.get("https://webdevbootcamp-jay-jayantamgr.c9users.io/api/risks", (error, response, body) => {
    if(error)
    {
        return console.dir(error);
    }
    var con = JSON.parse(body);

    var highImpact = Math.max.apply(Math, con.map(i => i.impact));


    var highProb = Math.max.apply(Math, con.map(p => p.propability));



    let dictRPZ = [];
    function addRPZ(RiskName, RPZ){
        dictRPZ.push({RiskName, RPZ});
    }

    con.forEach(function(item){
        var RPZ = item.impact * item.propability;
        addRPZ(item.title, RPZ);
    });

});

Expected result is to make a Dashboard and show the risk with highest impact, highest RPZ, live table of the risks and a moving average chart

Upvotes: 0

Views: 57

Answers (3)

Mark
Mark

Reputation: 92440

You can find the max value by applying the formula in a reduce() loop always returning the max (by whatever criterion you want). This is more efficient than sorting the entire array since you only need a single pass and you don't create a new array just to throw it away.. It will return the object from the array meeting the criteria you set:

let arr = [
    { "id": 3, "impact": 7, "probability": 0.7, "title": "Risk 3" },
    { "id": 4, "impact": 4, "probability": 0.72, "title": "Risk 4" },
    { "id": 5, "impact": 1, "probability": 0.6, "title": "Risk 5" },    
    { "id": 6, "impact": 8, "probability": 0.91, "title": "Risk 6" }
]

// max impact * probability
let max = arr.reduce((max, current) => 
    current.impact * current.probability > max.impact * max.probability
    ? current
    : max
)
console.log(max)

For a single value it's the same idea, but simpler:

let arr = [
    { "id": 3, "impact": 7, "probability": 0.7, "title": "Risk 3" },
    { "id": 4, "impact": 4, "probability": 0.72, "title": "Risk 4" },
    { "id": 5, "impact": 9, "probability": 0.1, "title": "Risk 5" },    
    { "id": 6, "impact": 8, "probability": 0.91, "title": "Risk 6" }
]

// max impact
let max = arr.reduce((max, current) => 
    current.impact > max.impact 
    ? current
    : max
)
console.log(max)

Upvotes: 0

Code Maniac
Code Maniac

Reputation: 37755

You can do it like this with sorting in descending order and selecting the first element of sorted array.

let arr = [
  { "id": 3, "impact": 7, "probability": 0.7, "title": "Risk 3" },
  { "id": 4, "impact": 8, "probability": 0.8, "title": "Risk 4" }
]

let highestImpact = arr.sort((a,b)=>b.impact-a.impact)[0].title;

console.log(highestImpact)

For highest RPZ you can do it like this

let arr = [
      { "id": 3, "impact": 7, "probability": 0.7, "title": "Risk 3" },
      { "id": 4, "impact": 8, "probability": 0.8, "title": "Risk 4" },
      { "id": 5, "impact": 10, "probability": 0.8, "title": "Risk 5" }
]

let highestRpz = arr.map(e=> {
 e.rpz= e.impact * e.probability
 return e;
 }).sort((a,b)=>b.rpz-a.rpz)[0].title

    console.log(highestRpz)

Upvotes: 2

Roi
Roi

Reputation: 1003

Another example:

  function findHighest(arr){
    var highest = arr[0];

    for(let i = 0; i < arr.length; i++) {
        let current = arr[i]
        let rpz = current.impact * current.probability;
        if(rpz > (highest.impact * highest.probability)) {
            highest = current
        }
    }
    return highest.title
}

Upvotes: 0

Related Questions