Change a Boolean Value of true to an array using Angular4 (Typescript)

My microservice is providing me with a series of boolean values.

   "mon": true,
   "tues": false,
   "wed": false,
   "thurs": true,
   "fri": false,
   "sat": true,
   "sun": false,

I want to take the values marked as true and convert them into an array like this:

options = ['mon', 'thurs', 'sat']

What would be the best approach to accomplish this task?

Answer №1

To filter out the days key from an object, you can utilize the filter method:

var week = {
  "mon": true,
  "tues": false,
  "wed": false,
  "thurs": true,
  "fri": false,
  "sat": true,
  "sun": false
};

var filteredDays = Object.keys(week).filter(day => week[day]);
console.log(filteredDays);

Answer №2

If you want to narrow down the keys:

const obj = {
    "summer": true,
    "autumn": false,
    "winter": false,
    "spring": true,
};
const selectedSeasons = Object.keys(obj).filter(key => obj[key]);

console.log(selectedSeasons);

Answer №3

Exploring a new method of working with entries and array breaking down 🚀🚀

let week = {
  "mon": true,
  "tues": false,
  "wed": false,
  "thurs": true,
  "fri": false,
  "sat": true,
  "sun": false
};

let selectedDays = Object.entries(week).filter(([key, state]) =>state).map(([key]) => key);

console.log(selectedDays);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Incorporating quotes into a unified npm script

I'm trying to merge two npm scripts into one, but the result is incorrect and causes issues with passing flags. I can't use the dotenv package, and using ampersands isn't solving the problem. Here's what I have in my package.json file ...

Creating a new model in TypeScript may encounter a declaration issue with getting

I may just be overlooking something, but I have the following model: import { Brand } from './brand'; import { Plan } from './plan'; import { Venue } from './venue'; export class Subscription { id: number; brandId: number ...

What is the issue when using TypeScript if my class contains private properties while the object I provide contains public properties?

I am currently facing an issue while attempting to create a typescript class with private properties that are initialized in the constructor using an object. Unfortunately, I keep encountering an error message stating: "error TS2345: Argument of type &apos ...

Passing the product ID from Angular to another function in order to retrieve the corresponding product image ID

I'm trying to figure out how to send the ID of a product from a database to the getImage function. I want the function to use this ID to find and display the image associated with that product. HTML <div class="container-fluid first" style="cur ...

The issue arises when specifying a type in a const method but neglecting to declare it in a regular function

While I was working on the layout, I checked out the official NextJS document for guidance. https://nextjs.org/docs/basic-features/layouts // _app.tsx export type NextPageWithLayout<P = {}, IP = P> = NextPage<P, IP> & { getLayout?: (page ...

Tips for incorporating a hybrid quicksort method into your programming algorithm?

I am currently working on developing a hybrid quick sort algorithm that utilizes the first element as a pivot. When the partition size falls below 100, I switch to using an insertion sort to complete the process. My code successfully sorts the data, howe ...

Redirect all traffic in Node.js/Express to an Angular 2 page

Currently working on a project using Angular 2, Node.js, and Express. Encountering an issue with my routes file when using a wildcard. Whenever I try to access a page other than / (such as /test), it throws the error message: ReferenceError: path is no ...

"Troubleshooting: Why Angular2's ngOnChanges is not triggering with array input

I am currently using a ParentComponent to pass inputs to a ChildComponent. When the input is a number, the ngOnChanges hook successfully fires. However, when it's an array, the hook does not trigger. Could someone help me identify what I might be doi ...

Extract the ID from the Router's URL

I am currently working on a project where I need to keep a log of every page that is accessed in my Angular application. Here is an overview of my routing setup: const routes: Routes = [ { path: ':id', component: AppComponent} ]; Within my a ...

Why does Angular perform a full tree check during local changes in change detection?

Imagine a scenario where there is a straightforward array of texts: contentList = [ {text: 'good morning'}, {text: 'lovely day'}, {text: 'for debugging'}, ] A simple component is rendered for each item in the a ...

How to showcase and randomize a data set in a Next.js application

Is there a way to dynamically change the content of a single "Card" component on every page refresh? I want to pull data such as title, subtitle, etc from an array in a data.js file and have it display different content randomly each time. Currently, I am ...

What is the best way to utilize card-group in conjunction with ngFor?

Here is the essential syntax for a card-group layout: <div class="card-group"> <div class="card"> <img class="card-img-top" [src]="getImage(1)" alt="Card image cap"> <di ...

JSON Error: Attempting to access the value of a key that is undefined

I've been working with an API and managed to successfully load a JSON array of objects in my browser. Here's a snippet of what it looks like: 0: source: {id: null, name: "Protothema.uk"} author: "james bond" title: " A TITLE" description: "A DES ...

What is the process for passing information to a nested component structure with parent-child-child relationships?

I am facing an issue with three nested components (C1, C2, C3) where C2 is called within C1 and C3 is called within C2. My goal is to pass data from C1 to C3 using property binding. In the template of C1, I successfully bound a variable that I can access ...

Tips for isolating shared attributes within MUI Data Grid column configurations

Currently, I am developing a ReactJS Typescript Application using MUI as my component library. My goal is to create a comprehensive CRUD Datagrid similar to the MUI Datagrid component. In the example provided, many columns share common properties. To effic ...

Aggregate the data from several multidimensional arrays based on the date column and calculate the total of another column in each group

I am looking to combine and categorize the information from 3 multidimensional arrays based on a common date column. Within these categories, I aim to add up the values in the Unit column to create a single 2d array with rows that have unique dates and tot ...

Utilizing an external HTTP API on an HTTPS Django and Angular server hosted on AWS

Can this be achieved? I am in the process of creating an ecommerce platform that necessitates interacting with an external API service built on HTTP. My website is hosted on AWS EBS, utilizing django for the backend and angular2 for the frontend. Whenever ...

When a user clicks on a React listItem, the information for that specific item is displayed using

As a beginner in the coding world, I am currently learning about React and JSON. My project involves working on three interconnected panels. Specifically, I aim to showcase checklist answers on the third panel. First Panel: Displaying: All the ESN ("46 ...

Is it possible to configure a unique Bearer Access Token in the "angular-oauth2-oidc" library?

For my Facebook login, I have set up a custom endpoint where the client sends the Facebook access token. In my Ionic App, I use the '@ionic-native/facebook/ngx' package to retrieve this token. Within a Laravel Json API controller, I utilize Soci ...

Create a custom component that wraps the Material-UI TextField component and includes default

I have been exploring React and had a question regarding wrapping a component like TextField from mui to add new props along with the existing ones. I attempted to do this but am struggling to figure out how to access the other props. Can anyone help me ...