Finding the hidden treasure within a nested array and transforming a boolean gem into a dazzling string - the ultimate quest!

How do I convert the values in the "active" field to "Yes" if true and "No" if false?

[ { "id":81, "time":"2022-01-01 19:30:00", "subList":[ { "active":false, "success":null } ] }, { "id":89, "time":"2022-01-01 21:00:15", "subList":[ { "active":true, "success":1 } ] } ]

Answer №1

Using Array.map Method

To iterate through the elements of an array, utilize the Array.map method or any other looping mechanism available.

If you want to simplify a basic if-else block, consider using the conditional ternary operator represented by `?` and `:` here.

const items = [ { "id":81, "time":"2022-01-01 19:30:00", "subList":[ { "active":false, "success":null } ] }, { "id":89, "time":"2022-01-01 21:00:15", "subList":[ { "active":true, "success":1 } ] } ];

const processedItems = items.map(item => {
    item.subList.map(subItem => {
        subItem.active = subItem.active ? "Yes" : "No";
        return subItem;
    });
    return item;
});

console.log(processedItems)

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

How to include extra data in Angular Firebase user creation using the createUserWithEmailAndPassword

Currently, I am working on implementing the Firebase createUserWithEmailAndPassword method. However, I would like to include an additional field named 'name' in Cloud Firestore. Below is a snippet of my code: auth.service.ts SignUp(email: string ...

Copy array in C or C++ column-wise

I have a collection of text files containing matrices that I can read and save as needed. However, there are times when I need to remove specific columns from the matrix. While nested loops can accomplish this, I believe there should be a way to achieve th ...

What causes the object type to shift away from 'subscribe'?

Currently, I am facing an issue with retrieving a Coupon object from the server using a REST API in combination with Angular. The problem arises when I attempt to access the 'subscribe' method - within the 'subscribe', the object is of ...

Looking to observe a decorated method using Jest even if the decorator poses a hurdle to method execution?

In my TypeScript project, decorators are a crucial part of enhancing method behavior. However, I recently ran into a situation where a decorator could potentially block the execution of a method. To test this scenario using Jest, I aim to confirm whether t ...

When running `ng build --prod`, the error message "Comparison operator '==' cannot be used with string and number types" is displayed

While working on my Angular 4 project, everything seemed to be running smoothly in the developer server without using the --prod flag. However, when I tried running ng build --prod, I encountered the following errors: Note: The project was built using ng ...

Develop a basic encryption program using PHP that involves substituting characters

Looking to create a simple encryption function in PHP as a newcomer to the language. The idea is to transform a string by changing the characters, utilizing two arrays. The first array holds all characters in a sorted order: $true_chars = array('a& ...

Error message: TscToolPath is missing in the Microsoft.TypeScript.targets file following the installation of Visual Studio 2015 update 3

Encountering an error in Visual Studio 2015 after updating to version 3 and using TypeScript 2.0. Error Invalid command line switch for "tsc.exe". Value cannot be null. Parameter name: path1 ProjectTest C:\Program Files (x86)\MSBuild&bso ...

Using Scala to parse a JSON file containing an embedded object and transforming that object into a usable format

I'm currently working on a project that involves parsing JSON and converting it into an object. The main challenge I'm facing is that the object contains an array of nested objects. Here's what I have so far: case class Account(number: Str ...

An array containing concatenated values should be transferred to the children of the corresponding value

Consider this example with an array: "items": [ { "value": "10", "label": "LIMEIRA", "children": [] }, { "value": "10-3", "label": "RECEBIMENTO", ...

What is the best way to reset a looping variable in a Node.js application?

I have encountered an issue with an API method in my nodejs app. Within one of my controllers, I am attempting to generate pages upon clicking. However, I have found that when calling the method, my for loop fails to reset the values to their default state ...

Can you explain the distinction between these two methods of slicing in a numpy array?

My numpy array looks like this: X=array([[0, 44.0, 72000.0], [2, 27.0, 48000.0], [1, 30.0, 54000.0], [2, 38.0, 61000.0], [1, 40.0, 63777.0], [0, 35.0, 58000.0], [2, 38.0, 52000.0], [0, 48.0, 79000.0], [1, 50.0, 83000.0], [0, 37. ...

I encountered the error message "The property you are trying to access does not exist on the type 'never'" while attempting to retrieve JSON data from the API response

Every time I attempt to access the fields in items such as id or title, an error pops up saying Property does not exist on type 'never'. Interestingly, when I log the data using console.log(response.data), everything seems to be working fine. I ...

Implementing chance.js in an Angular 4.x Component's ngOnInit() Method

I just started using angular 4.x and I'm trying to load change.js in the ngOnInit() function. I've attempted a few things but nothing seems to be working. Here is the code snippet: This is the code I have so far: import { Component, OnInit, In ...

Ways to shift duplicates to the beginning of an Object array?

I am working with a variable that contains an array of objects let Obj1 = [ {Id: 123, name: 'A'}, {Id: 124, name: 'B'}, {Id: 125, name: 'C'}, {Id: 126, name: 'D'}, {Id: 127, name: 'E&apo ...

Generate a three-dimensional array with irregular edges in Java

I am looking to create a unique ragged 3d array in Java with the following characteristics. Definition: A 2D array is designated by rows and columns. A 3D array comprises slabs, each of which contains a 2D array. The first slab has three rows, the second ...

Create an instance of a class from a group of subclasses, all the while retaining the ability to access static members in Types

I seem to have encountered a dilemma where I am looking to have both the static and abstract keywords used for a member of an abstract class in TypeScript, but it appears that this combination is not supported. The nearest workaround I could come up with ...

What is causing the issue with entering the code? Exploring the restrictions of the mui tag

Can someone explain why I am unable to use boolean data type in this code snippet? I am trying to determine whether the user is an admin or not, and based on that, hide or disable a button. const Authenticate = useSelector(userSelector) let checkValue ...

Function modifies global variable

What could be causing the global variable to change when using the function write_ACK_ONLY()? I'm passing the array rxUartBuffer to write_ACK_ONLY() as data = new Array(20), but upon checking the Log Output, it seems that the function is also modifyin ...

"Exploring the best practice: Defining types in React with Typescript before or after

As a newcomer to typescript, I have noticed that some projects declare the type before the component while others declare it after the component. Can someone explain the differences between these approaches? export type TProps = { item: string; } expor ...

Unable to compile Webpack due to aws-jwt-verify issue

Trying to package node code for an AWS lambda function with lambda@edge, and running into an error. Module not found: Error: Can't resolve 'aws-jwt-verify/error' Typescript compiles successfully with TSC, but webpack throws this error durin ...