最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to retrieve last element id of array in Angular 2 - Stack Overflow

programmeradmin2浏览0评论

I am trying to get id of last element of my array.

This is how I am fetching last element of my array

let last = this.Array[this.Array.length-1];
console.log(last);

Here is the console of last element of array-

Object {id: 48, title: "I'm used to this"}
 title: "I'm used to this"
 id:  48
__proto__:  Object

Here is the list on which I have looked already-

How to retrieve the clicked ElementId in angularjs?
How to get the object's id?
how to get the id name in html for any object and many more but I could not.

I just wanted to access id : 48, can any one help me to do so.
Thanks In Advance.

I am trying to get id of last element of my array.

This is how I am fetching last element of my array

let last = this.Array[this.Array.length-1];
console.log(last);

Here is the console of last element of array-

Object {id: 48, title: "I'm used to this"}
 title: "I'm used to this"
 id:  48
__proto__:  Object

Here is the list on which I have looked already-

How to retrieve the clicked ElementId in angularjs?
How to get the object's id?
how to get the id name in html for any object and many more but I could not.

I just wanted to access id : 48, can any one help me to do so.
Thanks In Advance.

Share Improve this question edited Sep 13, 2018 at 12:18 aloisdg 23.5k7 gold badges92 silver badges111 bronze badges asked Jun 14, 2017 at 7:19 S.YadavS.Yadav 4,5093 gold badges39 silver badges44 bronze badges 4
  • 1 What about console.log(last.id);? – Günter Zöchbauer Commented Jun 14, 2017 at 7:21
  • It shows this- Error TS2339: Property 'id' does not exist on type 'Object'. – S.Yadav Commented Jun 14, 2017 at 7:24
  • Seems more a problem with your linter configuration, or use concrete classes and interfaces instead of Object if you have such strict linter rules configured. – Günter Zöchbauer Commented Jun 14, 2017 at 7:27
  • Thanks all, It is working for me, it was issue with declare part, initially it was- export class classname{ myArray: <object> } which I just changed with export class classname{ myArray: <any> } and it's working fine now. – S.Yadav Commented Jun 14, 2017 at 8:24
Add a comment  | 

2 Answers 2

Reset to default 11

Just do that:

let last:any = this.Array[this.Array.length-1];
console.log(last.id);

You can do this.Array.slice(-1)[0].id, and with slice your original array is not changed too.

DEMO :

var array = [{
  id: 43,
  title: "I'm used to this"
},{
  id: 44,
  title: "I'm used to this"
},{
  id: 45,
  title: "I'm used to this"
},{
  id: 46,
  title: "I'm used to this"
},{
  id: 47,
  title: "I'm used to this"
},{
  id: 48,
  title: "I'm used to this"
}]

console.log('last id : ' + this.array.slice(-1)[0].id)

Update:

As your Array item is type of Object, So first convert it to some real class\interface type, Something like:

export  interface arrayItem{
    id?: number;
    title?: string;
}

then define your array type, like :

your Array like

this.Array : Array<arrayItem> = [ {
  id: 48,
  title: "I'm used to this"
  },
  //your other array items
]
发布评论

评论列表(0)

  1. 暂无评论