Filter array of objects by array of ids

You can simply use array.filter with indexOf to check the matching element in the next array. var arr = serviceList.filter(item => activeIds.indexOf(item.id) === -1); DEMO let activeIds = [202, 204] let serviceList = [{ “id”:201, “title”:”a” }, { “id”:202, “title”:”a” }, { “id”:203, “title”:”c” }, { “id”:204, “title”:”d” }, { “id”:205, “title”:”e” }]; let arr … Read more

NestJs: Unable to read env variables in module files but able in service files?

Your .env file is not yet read in when your JwtModule is instantiated. So either read it in earlier e.g. in your main.ts before the nest app is created or better: create a ConfigService and make the dependency on your config explicit: JwtModule.registerAsync({ imports: [ConfigModule], useFactory: async (configService: ConfigService) => ({ secret: configService.jwtSecret, }), inject: … Read more

How to properly reset Vue Composition Api’s reactive values

You can use Object.assign: setup() { const initialState = { name: “”, lastName: “”, email: “” }; const form = reactive({ …initialState }); function resetForm() { Object.assign(form, initialState); } function setForm() { Object.assign(form, { name: “John”, lastName: “Doe”, email: “[email protected]” }); } return { form, setForm, resetForm }; } See it live on codesandbox credits: … Read more

How to escape HTML

I’m very surprised no one answered this. You can just use the browser it self to do the escaping for you. No regex is better or safer than letting the browser do what it does best, handle HTML. function escapeHTML(str){ var p = document.createElement(“p”); p.appendChild(document.createTextNode(str)); return p.innerHTML; } or a short alternative using the Option() … Read more

How to forbid a specific named function with ESlint

You can do some very fancy things when you combine ESLint’s no-restricted-syntax rule with selectors. In your case you should be able to achieve the linting you want without writing your own plugin. The rule would look something like: { rules: { ‘no-restricted-syntax’: [ ‘error’, { message: “Please don’t use asMutable. We don’t like it!”, … Read more

Refactor this method to reduce its Cognitive Complexity from 21 to the 15 allowed. How to refactor and reduce the complexity

A little information on how cognitive complexity works and why you should keep it low First of all it is important to understand how “Cognitive Complexity” works as compared to “Cyclomatic Complexity“. Cognitive complexity takes into account the complexity perceived by the human brain. This is why it does not simply indicate the number of … Read more