3

I'm having JSON data source as like below,

var datasource = [{name: 'x', age: 20, address: 'yyyy'},
                  {name: 's', age: 30, address: 'zzzz'}]

in this fields(name, age, address) will be dynamically changed as per the user. Here i want to iterating the records and its values.

I am trying like this and facing exception,

let keys: Object[] = Object.keys(datasource[0]);
let dlen: number = datasource.length as number;
while (dlen--) {
    let kLn: number = keys.length;
    while (kLn--) {
        let key: string = keys[kLn] as string;
        let isPropContains: boolean = false;
        let value: string;
        if(datasource[dlen][key]) {
             isPropContains = true;
             value = datasource[dlen][key] as string;
        }
    }
}

Here, how can i access object property (datasource[dlen][key]) like we accessing in javascript index type?

Note: I want to achieve this without using type 'any' and 'eval' in typescript. Need strongly typed.

I have checked this How do I dynamically assign properties to an object in TypeScript? but it doest satisfied my requirement. Because they using 'any' type, i want to achieve without using 'any' or 'eval'.

Pranath
  • 321
  • 1
  • 4
  • 13
  • If you had simply pasted the error into any half-decent search engine, you would have found the linked question and its answers. – T.J. Crowder Jan 16 '18 at 12:59
  • @T.J.Crowder: I have edited my question. I need to access object property like index signature, without using 'any' type. Already i have checked that post, it won't help me. – Pranath Jan 17 '18 at 04:56
  • Applying [the first answer](https://stackoverflow.com/a/32968953/157247) to that question to your code: https://pastebin.com/cMg2WdG9 The only trick was that `age` is `number` and the other two are string. The playground (with the `noImplicitAny` option enabled) has no complaints about that code (too big to link, but if you copy-and-paste and set the option...). – T.J. Crowder Jan 17 '18 at 07:30
  • @T.J.Crowder: In my case datasource is not static, So 'age' and 'name' is just an example that i provided. Properties may differ with different type. This is dynamic. So i can't create interface for that datasource. – Pranath Jan 17 '18 at 09:29
  • So you have a datasource with a varying structure, and you want to apply typechecking to it. I don't see how that works logically...? If you can't define a structure for it, how are you going to apply typechecking for it? – T.J. Crowder Jan 17 '18 at 09:47
  • Sorry @T.J.Crowder. I just want to know weather property available to that object and i need to access that value. Value will be number | string | Date. – Pranath Jan 17 '18 at 10:46
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/163315/discussion-between-pranath-and-t-j-crowder). – Pranath Jan 17 '18 at 10:50
  • Then you'll need to do what's in the pastebin above (minus the actual fields, e.g.: https://pastebin.com/F1LS5Saa). `string|number|Date` is better than `any` for typechecking I suppose! :-) – T.J. Crowder Jan 17 '18 at 10:51
  • @T.J.Crowder: Before that, I can not access the property as like array index signature. It throws exception " Element implicitly has an 'any' type because type 'Object' has no index signature" https://ibb.co/fqzoRm – Pranath Jan 17 '18 at 11:06
  • 1
    Please look closely at the code I linked. `Object` isn't used, and TypeScript doesn't have any complaints about that code (even with the noImplicitAny option set). – T.J. Crowder Jan 17 '18 at 11:09
  • 1
    Thanks @T.J.Crowder. Your trick works for me. – Pranath Jan 17 '18 at 13:00

0 Answers0