从数组中取出值时变得不确定

时间:2019-05-09 13:18:11

标签: javascript

我正在尝试从数组中打印单个值,但它似乎不起作用(获取未定义)。当我打印出数组本身时,它表明值包含在数组中的索引0和1处。

let imgDim = [];
imgDim.push(300);
imgDim.push(300);
let width = imgDim[0];
let height = imgDim[1];
let testString = "s994320";
if (/^[ms]\d+$/.test(testString) == true) {
    console.log(imgDim); //works
    console.log(width, height); //undefined undefined
}

3 个答案:

答案 0 :(得分:1)

在让testString = s994320时出错;

let imgDim = [];
imgDim.push(300);
imgDim.push(300);
let width = imgDim[0];
let height = imgDim[1];
let testString = 's994320';
if (/^[ms]\d+$/.test(testString) == true) {
  console.log(imgDim); //works
  console.log(width, height); //undefined undefined
}

let imgDim = [];
imgDim.push(300);
imgDim.push(300);
let width = imgDim[0];
let height = imgDim[1];
let testString = 's994320';
if (/^[ms]\d+$/.test(testString) == true) {
  console.log(imgDim); //works
  console.log(width, height); //undefined undefined
}

答案 1 :(得分:1)

通过将引号添加到 s994320 中,代码可以正常工作:

<ng-container>
  <td>
    some other code

     <ng-container *ngIf="riga.value['type'] === 'title';else other">
       {{riga.value['description']}}
    </ng-container>

    <ng-template #other>
      <ng-container>
         {{riga.value['otherdescription']}}

      </ng-container>
    </ng-template>

 </td>
</ng-container>

答案 2 :(得分:0)

const imgDim = [];

imgDim.push(300);
imgDim.push(300);

const [
  width,
  height,
] = imgDim;

const testString = 's994320';

if (/^[ms]\d+$/.test(testString)) {
  console.log(imgDim);
  
  console.log(width, height);
}