如何在typescript中创建变量时传递属性值中的变量

时间:2017-05-05 16:39:01

标签: javascript arrays angular object typescript

this.listvalue;
list.forEach((v: any) => {

  v.data.forEach((v2: any) => {


    this.searchlist.push({
      header: v.header,
      value: v2.value_0

    });
  });
});

我想在this.listvalue的地方传递v2.value_0 =>v2.this.listvalue,但这不起作用,语法错误我也尝试了v2.valueof(this.listvalue),但它也不能正常工作我想改变通话v2通过this.listvalue

  

我该怎么做?这种情况的正确语法是什么?   整个代码在这里

export class Md2Component {
  @Input() list: any;
  @Input() listvalue: any;

  public searchlist;
  public placeholder: String = "Searct the JSON";
  public switch: boolean = true;
  public switch5: boolean = true;
  public searchlistresult: any
  public data: string;
  public header1: String;
  public resultant_list: any;
  public lastone: user[] = [];
  @Output() _onSelect = new EventEmitter<any>();
  public anyinstance: any
  public user2: user

  s = '';
  public index;

  constructor() {

  }
  ngOnInit() {
    this.data = this.listvalue;
    this.createSearchList(this.list);
    this.latest(this.searchlist);

  }
  private createSearchList(list: any) {
    this.searchlist = [];
    this.listvalue;
    list.forEach((v: any) => {
      v.data.forEach((v2: any) => {
        this.searchlist.push({
          header: v.header,
          value: v2.value_0

        });
      });
    });



  }
  search1(s) {
    this.search(s);
    if (this.switch != true) {
      this.latest(this.searchlistresult)
    }
  }

  search(s) {
    this.switch = false;

    this.searchlistresult = _.filter(this.searchlist, (o: any) => {
      if (s) {

        return _.startsWith(_.lowerCase(o.value), _.lowerCase(s));
      }
      this.switch = true;
      return true;
    });
  }
  latest(list: any) {

    const arr = list;

    const keys = [];

    for (let i of arr) {
      if (!keys.includes(i.header)) keys.push(i.header);
    }

    const result = keys
      .map(k => Object.assign({header: k}, {
        data: arr.filter(x => x.header === k)
          .map(y => Object.assign({}, {value: y.value}))
      }));
    this.anyinstance = result;



  }
  generateArray(obj) {
    return Object.keys(obj).map((key) => {return obj[key]});
  }
  onSelect(header: any, value: any) {
    {
      console.log(header);
      console.log(value);
      this.s = value;
      this.user2 = {
        header: header,
        value: value
      }
      this.lastone.push({
        'header': header,
        'value': value,
      }
      );
      this.switch = true;
      this._onSelect.emit(this.user2);
    }
  }
}

1 个答案:

答案 0 :(得分:0)

您已经说过this.listvalue@Input(),所以我们假设它已经用值初始化了。

更新您可以尝试作为参数传递。由于listvalue的全球范围是导致您的问题的原因,将其传递到函数中会使其进入正确的范围。

// Bringing listvalue into scope, to avoid use of 'this' accessor.
private createSearchList(list: any, listvalue: any) { 
    this.searchlist = []; 

    list.forEach((v: any) => { 
        v.data.forEach((v2: any) => { 

            this.searchlist.push({ 
                header: v.header, 
                value: listvalue
            }); 
        }); 
    }); 
 }