'Console log an interface without some object
Do you know how can I console log the interface example without the m and n? Below the two interface is in one file.
export interface example {
readonly a: string;
readonly b: F | undefined;
readonly c: G | undefined;
readonly d?: boolean;
readonly e: H | undefined;
}
export interface F {
readonly i: string;
readonly j: q;
readonly k: string | undefined;
readonly l: r;
readonly m: s | undefined;
readonly n: t | undefined;
readonly o: y | undefined;
readonly p: v[] | undefined;
}
Below is in another file
import { example } from 'src/modules/ex';
exampleInfo: example | undefined;
console.log( exampleInfo.a + exampleInfo.b.i + exampleInfo.b.j + exampleInfo.b.k + exampleInfo.b.l + exampleInfo.b.o + exampleInfo.b.p + exampleInfo.c + exampleInfo.d+ exampleInfo.e)
Currently the console log is super long and easy to make mistake. Also I want to include the key(a,b,c,o,p...etc).not just the value. If I simply type
console.log(exampleInfo);
I can get the result I want (with all key and value pair). However, it's included the m&n that I don't want.
I've also tried to destruct it.
const {m,n, ...otherChar} = exampleInfo;
But it gives me error:
"Property 'm' does not exist on type 'example | undefined'.
Or
const {F.m,F.n, ... otherChar} = exampleInfo;
But still error:
"Property 'F' does not exist on type 'example | undefined'.
Also tried this:
const {m,n, …otherChar} = exampleInfo.b;
Error changed to:
Property 'm' does not exist on type 'F | undefined'.
How should I destruct it? or is there any other way to log them??
Solution 1:[1]
Try console.log(JSON.stringify(example))
Edit
Sorry I didn't see the requirement clearly. Not sure this is optimal solution, but hope it helps.
let b = Object.fromEntries(Object.entries(example.b).filter(x => x[0] != 'm' && x[0] != 'n'));
const result = {...example};
result.b = b;
console.log(JSON.stringify(result));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
