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

Javascript 在typescript中创建变量时如何在属性值中传递变量,javascript,arrays,angular,object,typescript,Javascript,Arrays,Angular,Object,Typescript,我想将this.listvalue传递到v2.value\u 0=>v2.this.listvalue的位置,但这不起作用,因为出现语法错误我也尝试了v2.valueof(this.listvalue),但它也不起作用,我想通过this.listvalue调用v2 我该怎么做?此场景的正确语法是什么? 全部代码都在这里 导出类MD2组件{ @Input()列表:任意; @Input()列表值:任意; 公共搜索列表; 公共占位符:String=“Searct the JSON”; 公共开关:布尔值

我想将
this.listvalue
传递到
v2.value\u 0=>v2.this.listvalue
的位置,但这不起作用,因为出现语法错误我也尝试了
v2.valueof(this.listvalue)
,但它也不起作用,我想通过
this.listvalue
调用v2

我该怎么做?此场景的正确语法是什么? 全部代码都在这里

导出类MD2组件{
@Input()列表:任意;
@Input()列表值:任意;
公共搜索列表;
公共占位符:String=“Searct the JSON”;
公共开关:布尔值=true;
公共开关5:boolean=true;
公共搜索列表结果:任何
公共数据:字符串;
公共标题1:字符串;
公用事业单位名单:有;
公共lastone:用户[]=[];
@Output()\u onSelect=neweventemitter();
公共任意实例:任意
公共用户2:用户
s='';
公共索引;
构造函数(){
}
恩戈尼尼特(){
this.data=this.listvalue;
this.createSearchList(this.list);
this.latest(this.searchlist);
}
私有createSearchList(列表:任意){
this.searchlist=[];
这就是价值;
list.forEach((v:any)=>{
v、 data.forEach((v2:any)=>{
这是searchlist.push({
标题:v.header,
值:v2.value\u 0
});
});
});
}
搜索1(s){
这是一个搜索;
if(this.switch!=true){
this.latest(this.searchlistresult)
}
}
搜寻{
这个开关=假;
this.searchlistresult=\过滤器(this.searchlist,(o:any)=>{
若有(s){
返回u.startsWith(u.lowerCase(o.value),u.lowerCase(s));
}
this.switch=true;
返回true;
});
}
最新(列表:任何){
const arr=列表;
常量键=[];
对于(让我考虑arr){
如果(!keys.包括(i.header))键,则按(i.header);
}
常量结果=键
.map(k=>Object.assign({header:k}){
数据:arr.filter(x=>x.header==k)
.map(y=>Object.assign({},{value:y.value}))
}));
this.anyinstance=结果;
}
发电机轨道(obj){
return Object.keys(obj.map)((key)=>{return obj[key]});
}
onSelect(标题:任意,值:任意){
{
console.log(头文件);
console.log(值);
这个.s=值;
this.user2={
标题:标题,
价值:价值
}
这个,最后一个,推({
“头”:头,
“值”:值,
}
);
this.switch=true;
this.onSelect.emit(this.user2);
}
}
}

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

更新您可以尝试作为参数传递。由于
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);
    }
  }
}

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

更新您可以尝试作为参数传递。由于
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);
    }
  }
}

你能添加完整的代码文件吗。。似乎您可能没有正确地声明
listvalue
“@Input()listvalue:any;”这是在我的代码顶部,我想它声明了` 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});});})}`删除后什么也不会发生。请用代码更新你的问题。你能添加完整的代码文件吗。。似乎您可能没有正确地声明
listvalue
“@Input()listvalue:any;”这是在我的代码顶部,我想它声明了` 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});});})}“删除它之后什么也没有发生。不管你有什么,请用代码更新你的问题……你搞错我了。。。我不想在V2.value\u 0中分配listvalue。实际上,我想在v2中调用类似于v2的listvalue中的attritube。listvalue@HASSANMEHMOOD您应该可以使用
v2.listvalue
no?执行此操作,但是list值不在函数范围内,我必须通过此函数调用它。listValuedDid
value:v2[this.listvalue]
是否为您工作?或者,您可以将正确的
listvalue
作为参数传递到此函数,以使其进入作用域。您搞错了。。。我不想在V2.value\u 0中分配listvalue。实际上,我想在v2中调用类似于v2的listvalue中的attritube。listvalue@HASSANMEHMOOD您应该可以使用
v2.listvalue
no?执行此操作,但是list值不在函数范围内,我必须通过此函数调用它。listValuedDid
value:v2[this.listvalue]
是否为您工作?或者,您可以将正确的
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
            }); 
        }); 
    }); 
 }