简体   繁体   English

仅显示Object.keys的前几个元素

[英]Display only the first elements of Object.keys

I have made a JSON Request so i can bring the Objects in Angular2. 我已经发出了JSON请求,因此可以将对象带入Angular2。 But I want to display only the first 15 elements and then if it works repeat the same process on InfiniteScroll. 但是我只想显示前15个元素,然后如果它起作用,请在InfiniteScroll上重复相同的过程。 So this is one of my code. 所以这是我的代码之一。

  setList(informes) {
  if (informes) {
     for (let id of Object.keys(informes)){
         this.count = 0;
         for (let i = 0; i < 15; i++) {
            let node = informes[id];
            this.informes.push(node[this.count]);
            console.log (id);
            this.count++;
          }
      }
   }
}

Obviously It doesn't work, it keeps giving me all elements like 15 times each. 显然这是行不通的,它不断给我所有元素,每次15次。 I know that but on the other hand if i make the opposite. 我知道,但另一方面,如果我相反。

setList(informes) {
  if (informes) {
     for (let i = 0; i < 15; i++) {
        for (let id of Object.keys(informes)){
        let node = informes[id];
        this.informes.push(node[this.count]);
        console.log (id);
        }
        this.count++
      }
   }
}

It counts the number of nodes in total. 它统计总数。

What i want is to display only the first 15 elements. 我想要的是仅显示前15个元素。 And then repeat the code in my other function infiniteScroll (I will do that by myself, it works). 然后在我的另一个函数infiniteScroll中重复代码(我将自己完成,它可以工作)。

Any suggestion will be appreciated. 任何建议将不胜感激。

UPDATE: 更新:

Here's the constructor: 这是构造函数:

 constructor(public navCtrl: NavController, public navParams: NavParams, public nav: NavController, public http: Http, public sanitizer: DomSanitizer) {
    this.dataUrl = 'https://myurl.com/ionic/'; //example
    if (this.dataUrl) {
         this.http.get(this.dataUrl)
            .map(res => res.json())
            .subscribe(informes => this.setList(informes));
         }
    }

UPDATE 2: 更新2:

The code works well. 该代码运行良好。 I had to modify some things to make it work. 我必须修改一些东西才能使其正常工作。 I will update the script so if it could help someone. 我将更新脚本,以便它可以帮助某人。

    setList(informes) {
  if (informes) {
     let ids = Object.keys(informes);
        ids.forEach((id, index) => {
           if(index < 15){
               let node = informes[id];
               this.informes.push(node);
               this.count++;
               console.log(this.count);
           }
      });
   }
}

goToNodeInformes(node){
   this.navCtrl.push(NodeInformesPage, {'node':node.nid});
}

doInfinite(infiniteScroll, informes) {
    informes = this.informes;
    setTimeout(() => {
      let ids = Object.keys(informes);
        ids.forEach((id, index) => {
           if(index < 15){
               let node = informes[id];
               this.informes.push(node);
               this.count++;
               console.log(this.count);
           }
      });
      infiniteScroll.complete();
    }, 500);
  }

} }

I will figure what i have to do for not repeating the same nodes (will update) but the counter works!!! 我将弄清楚不重复相同节点(必须更新)但计数器可以工作的原因!!!

I think you are looking for something like this : 我认为您正在寻找这样的东西:

let keys = Object.keys(informes);

keys.foreach((key, index) => {
   if(index < 15){
       let node = informes[key];
       this.informes.push(node);
       console.log(informes[key]);
   }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM