1

I have an object listData with the following values:

{
    "TestId": 2,
    "CurrentTestVersion": 1,
    "TestNumber": "2015-29059",
    "SharingData": 1.000000,
    "ThresholdValue": 0.0,
    "ExpireDate": "2022-12-31T00:00:00",
    "UpdateDate": "2021-10-01T00:00:00",
    "TestCurrency": "INR",
    "TestCode": "44300",
    "TestUCode": "",
    "IndexType": "LME",
    "IndexCode": "EUR",

}

I need to create one more object with only selected fields like:

{
    "TestId": 2,
    "CurrentTestVersion": 1,
    "ThresholdValue": 0.0,
    "ExpireDate": "2022-12-31T00:00:00",
    "TestCurrency": "INR",
    "TestCode": "44300",
    "TestUCode": "",
}

I have checked with What is the most efficient way to copy some properties from an object in JavaScript?, which works well with JavaScript but not with TypeScript; any better ways to selectively copy properties?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • What _specific problem_ have you had adapting those JS answers to your use case? The runtime behaviour is the same so it should just be a case of adding type information, no? Give a [mre]. – jonrsharpe Jan 06 '22 at 09:31
  • 1
    Could you use `Pick`? Docs: https://www.typescriptlang.org/docs/handbook/utility-types.html#picktype-keys – André Krosby Jan 06 '22 at 09:32

2 Answers2

1

I think you need a mapper something like this:

  const objMapper = (obj: { [key: string]: string | number }, keys: string[]) => {
    const result: { [key: string]: string | number } = {};

    for (const k of keys) {
      if (obj[k]) {
        result[k] = obj[k];
      }
    }

    return result;
  };

and you can call it like this:

const result = objMapper({
    "TestId": 2,
    "CurrentTestVersion": 1,
    "TestNumber": "2015-29059",
    "SharingData": 1.000000,
    "ThresholdValue": 0.0,
    "ExpireDate": "2022-12-31T00:00:00",
    "UpdateDate": "2021-10-01T00:00:00",
    "TestCurrency": "INR",
    "TestCode": "44300",
    "TestUCode": "",
    "IndexType": "LME",
    "IndexCode": "EUR",

}, ["TestId", "CurrentTestVersion", ...]);

and it will return your custom object

Mohsen Mahoski
  • 452
  • 1
  • 5
  • 14
  • Thanks for the suggestion, it works well with static JSON, but when I try with dynamic JSON object , the result is null. var PistData = this.TestDetails.userDetails; const result = this.objMapper({ PistData }, ["TestId"]) console.log("result pist:", result); – Kiran Narayan Jan 07 '22 at 02:48
  • Can you tell me what will be the result if you change ```result = this.objMapper({ PistData })``` to ```result = this.objMapper({ ...PistData }, ['TestId']);``` ? – Mohsen Mahoski Jan 07 '22 at 07:11
-1

You don't need to remove the extra property, since you are working with type script, you must have two interface/class to represent respective the structures. eg:

        AllProperty ap; // source
        SomeProperty sp; // target
        
  // Sine in your example your object does not have any reference type property , 
 // you can simply transfer the values to a new object.
        
        var sp = Object.assign(sp, ap) as SomeProperty;

If you want to remove properties for some other reason (data being sent through API)

you can either use 'delete' keyword, ... spread operator etc
vaira
  • 2,191
  • 1
  • 10
  • 15