Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #38406

Re: create 2 dimension tuple in Typescript and access items

Newsgroups comp.lang.javascript
Date 2020-05-12 14:09 -0700
References <e5d1fdb0-c8d9-4077-9cd0-445042c1d2d2@googlegroups.com>
Message-ID <6429b7fc-6ec8-4e2f-90b2-e3d9d0217e8f@googlegroups.com> (permalink)
Subject Re: create 2 dimension tuple in Typescript and access items
From "Michael Haufe (TNO)" <tno@thenewobjective.com>

Show all headers | View raw


On Saturday, May 9, 2020 at 6:49:42 PM UTC-5, JRough wrote:
> I have 3 rows of data , I want  individual items inserted into web page , but on the first line where I define the tuple , I get an error and can't compile typescript?
> 
> const productlist: Array( number | string| number|boolean| string| string)[];  *****

If it's supposed to be an array of tuples then the type should look like this:

```ts
const productlist: [number,string,number,boolean,string,string][] = [];
```

You'll need to fix the type of your populating though:

```ts
productlist.push(
    [431,"SamsungGalaxyNote7",59000,'Available',"url('MobileCart_Images_part1/SamsungGalaxy_Gold.jpg')","Samsung Galaxy Note 7 is a stylish mobile you can ever have. It has 64GB memory."],
    [433,'SamsungGalaxyS6Edge',53499,'Available',"url('MobileCart_Images_part1/samsung_edge_silver.JPG')", "Samsung Galaxy Edge blah blah blah"],
    [434,'NokiaLumiai640XL',14990,'OutofStock',"url('MobileCart_Images_part1/lumia_640xl.jpg')", "Nokia Lumia 640XL blah blah blah"]
);
```

> 
> And here is my script to insert the individual items into the html page:
> 
> for ( var i = 0; productlist.length >  i++){
>        if ( i = 0){
>          document.getElementById("pName0").innerHTML = productlist[i][1] ;
>          document.getElementById("pPrice0").innerHTML = productlist[i][2];
>          document.getElementById("pAvailable0").innerHTML= productlist[i][3];
>      } else if( i = 1) {
>         document.getElementById("pName1").innerHTML = productlist[i][1] ;
>          document.getElementById("pPrice1").innerHTML = productlist[i][2];
>          document.getElementById("pAvailable1").innerHTML= productlist[i][3];
>      }else {
>         document.getElementById("pName2").innerHTML = productlist[i][1] ;
>          document.getElementById("pPrice2").innerHTML = productlist[i][2];
>          document.getElementById("pAvailable2").innerHTML= productlist[i][3];    
> 
>     }
>    }
> 

Maybe instead something like:

```ts
productlist.forEach(([,name, price, available], i) => {
    document.getElementById(`pName${i}`).innerHTML = name
    document.getElementById(`pPrice${i}`).innerHTML = price
    document.getElementById(`pAvailable${i}`).innerHTML = available
})
```

Back to comp.lang.javascript | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

create 2 dimension tuple in Typescript and access items JRough <janis.rough@gmail.com> - 2020-05-09 16:49 -0700
  Re: create 2 dimension tuple in Typescript and access items JJ <jj4public@vfemail.net> - 2020-05-10 19:56 +0700
    Re: create 2 dimension tuple in Typescript and access items JRough <janis.rough@gmail.com> - 2020-05-10 06:09 -0700
  Re: create 2 dimension tuple in Typescript and access items Arno Welzel <usenet@arnowelzel.de> - 2020-05-10 22:56 +0200
  Re: create 2 dimension tuple in Typescript and access items JRough <janis.rough@gmail.com> - 2020-05-11 11:06 -0700
    Re: create 2 dimension tuple in Typescript and access items JRough <janis.rough@gmail.com> - 2020-05-11 11:52 -0700
  Re: create 2 dimension tuple in Typescript and access items "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2020-05-12 14:09 -0700

csiph-web