isawid
isawid

Reputation: 35

Type script get max array

I have an array with one column n_fnc and I would like to find the max value . I tried with this but I don"t get anything.

 let first = this.fncs.map(item => item.n_fnc);
   console.log("First",first);
    x= Math.max(...first);

[max aray

fnc service

getlastid(response,fncs:Fnc[]):void{
      let fnc:Fnc;
      response.forEach(element => {
        
        fnc =new Fnc();
        fnc.n_fnc=element.n_fnc;
        fncs.push(fnc);

    });

Fnc component.ts

this.fncs=[];
this.fncservice.obs.subscribe((response)=>this.fncservice.getlastid(response,this.fncs));
     console.log("A",this.fncs);
    var max= Math.max.apply(Math, this.fncs.map((m) => m.n_fnc));
    console.log("Max",max);

Upvotes: 0

Views: 68

Answers (2)

Nikhil Patil
Nikhil Patil

Reputation: 2540

You can use apply function to do that -

var fncs = [{
 n_fnc: 1
},{
 n_fnc: 499
},{
 n_fnc: 99
},{
 n_fnc: 10
}];

var max = Math.max.apply(Math, fncs.map((m) => m.n_fnc));
console.log(max);

The code block in fnc.component.ts should be like -

this.fncs=[];
this.fncservice.obs.subscribe((response)=> {        
    this.fncservice.getlastid(response,this.fncs);
    console.log("A",this.fncs);
    var max= Math.max.apply(Math, this.fncs.map((m) => m.n_fnc));
    console.log("Max",max);
});

Upvotes: 1

Ravi Ashara
Ravi Ashara

Reputation: 1196

You can use apply function to do that -

let fncs = [{
 n_fnc: 1
},{
 n_fnc: 499
},{
 n_fnc: 99
},{
 n_fnc: 10
}];

let mytemp = fncs.sort((a, b) =>  { return b.n_fnc > a.n_fnc ? 1 : -1; });

console.log(mytemp[0]);

Upvotes: 0

Related Questions