Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/424.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 拆分输入数组值-角度_Javascript_Html_Arrays_Angular_Typescript - Fatal编程技术网

Javascript 拆分输入数组值-角度

Javascript 拆分输入数组值-角度,javascript,html,arrays,angular,typescript,Javascript,Html,Arrays,Angular,Typescript,我有一个输入字段,用户可以在其中输入多个输入,中间使用逗号 <div class="container"> Enter your values:<input type="text" multiple #inputCheck> <input type="submit"(click)="sendInput(inputCheck.value)"> </d

我有一个输入字段,用户可以在其中输入多个输入,中间使用
逗号

<div class="container">
    
    Enter your values:<input type="text" multiple #inputCheck>
    <input type="submit"(click)="sendInput(inputCheck.value)">

</div>
我尝试过使用下面的代码,但是输入没有在数组中分割,整个输入被视为数组中的单个元素。我需要将输入分成多个元素,并将它们存储在数组中

     sendInput(event:any){
     this.inputGiven = event;
     this.arrayStored.push(this.inputGiven);

示例:如果用户输入
SAM,ALEX7,23
并单击提交,数组应将其存储为
arrayStored=[“SAM”,“ALEX7,23”]
,但现在存储为
arrayStored=[“SAM,ALEX7,23”]
。如何拆分输入并将其存储为数组中的单个元素?

您可以按如下方式拆分数组中的元素:

 this.arrayStored.concat(this.inputGiven.spilt(“,”));
要从阵列中删除任何重复项,您可以将其转换为一个集合,然后再转换回一个阵列,如下所示:

 this.arrayStored = Array.from(new Set(this.arrayStored));

可以按如下方式拆分数组中的元素:

 this.arrayStored.concat(this.inputGiven.spilt(“,”));
要从阵列中删除任何重复项,您可以将其转换为一个集合,然后再转换回一个阵列,如下所示:

 this.arrayStored = Array.from(new Set(this.arrayStored));

尝试
this.arrayStored=[…this.arrayStored,…this.inputGiven.split(“,”);
它工作了,但现在,如果我再次单击submit按钮,它会复制数组中的值。例如:[“SAM”、“ALEX7”、“23”、“SAM”、“ALEX7”、“23”]即使多次单击“提交”按钮,我也需要这些值只显示一次。对此有何想法?请尝试一下。
this.inputGiven.split(“,”).forEach(value=>{!this.arrayStored.includes(value)&&this.arrayStored.push(value)};
forEach(…)不可写(无法读取未定义的属性)
它不起作用。它未经测试。无论如何,你已经得到了答案。请尝试
this.arrayStored=[…this.arrayStored,…this.inputGiven.split(“,”);
它起作用了,但现在,如果我再次单击提交按钮,它会复制数组中的值。例如:[“SAM”、“ALEX7”、“23”、“SAM”、“ALEX7”、“23”]即使多次单击“提交”按钮,我也需要这些值只显示一次。对此有何想法?请尝试一下。
this.inputGiven.split(“,”).forEach(value=>{!this.arrayStored.includes(value)&&this.arrayStored.push(value)};
forEach(…)不可写(无法读取未定义的属性)
它不起作用。它未经测试。不管怎样,你得到了答案。非常感谢简化!非常感谢简化!