Obtaining a value based on another value within an object the AngularJS approach

Is there a specific AngularJS method to retrieve a value from an object using its key?

var myobj=   {
        "set1": {
            "key": "B11",
            "color": "yellow"
        },
        "setA": {
            "key": "F34",
            "color": "green"
        }
    }
    

For example, if I want to access the value "green" by providing the key "F34" in the object myobj, is there a built-in AngularJS way to achieve this or should I refer to resources like this SO question?

Answer №1

If you're looking to streamline this process across various controllers and services, consider creating a personalized filter to handle the task efficiently.

angular.module('myApp').filter('retrieveColor', retrieveColor);

function retrieveColor() {
    return filter;

    function filter(object, key) {
        var color;
        angular.forEach(object, function(set) {
            if (set.key === key)
                color = set.color;
        });

        return color;
    }
}

This custom filter can then be implemented as follows:

$scope.color = $filter('retrieveColor')(myobj, 'F34');

View demo on JSFiddle

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

What is the jQuery method for generating a new element?

Is this the correct way to create an element: $('<div />') or $('<div></div>') Can you confirm if this is the proper syntax for creating an element? Thank you. ...

Accessing Elements by Class Name

I've been searching for information on the capabilities of getElementsByClassName for hours. While some sources mention it returns an HTML collection of items, length, and named items, w3schools provides an example where innerHTML is utilized: var x ...

Updating a MongoDB document using only the data from checked checkboxes

Imagine having data structured like this: _id:60e2edf7014df85fd8b6e073 routineName:"test" username: "tester" monday:[{ _id: 60e430d45395d73bf41c7be8 exercise: "a" }{ _id: 60e4329592e ...

Strange behavior in Angular's http response

When I make a call to my API and receive a JSON response, the code snippet below illustrates how I handle it: getAllLearn() { this.learnService.getAllLearn().subscribe(res =>{ // The console log shows that res.featured only has one index: ( ...

Refresh Twitter Bootstrap Tooltip after deactivating/removing

I have a quick question. I am currently dealing with data that is constantly changing and displayed from a selected item in a table. To monitor for overflow, I have implemented the following code: if (event.target.offsetWidth < event.target.scrollW ...

Ways to prevent negative values from appearing in the text field

Check out this code snippet on Fiddle. I need help with a text field that should only display positive values. If the input is negative, I want it to be stored in ng-model but not shown in the textbox. function LoginController($scope) { $scope.number = ...

Custom ValueFilter causing ng-Repeat to malfunction (Value | customFilter) in version 1.3.x, unlike in version 1.2.1

I am facing a challenge while attempting to upgrade a Solution from version 1.2.1 to 1.3.15. It seems that there has been a change in how the digest cycle loops through. In my Factory, I store data for all my controllers, including a list of objects and a ...

Synchronize the scrolling of two tables sharing a common data source to ensure both tables scroll in unison

I encountered some issues while using ui-scroll in my application. My goal is to have ui-scroll function on the same datasource that is used to populate two tables. By scrolling one table, I want the other table created from the same data source to also s ...

Guide to building a personalized dropdown menu with user input using Ant Design Vue and Vue 3

I'm trying to implement a customized dropdown using antdv, but the example provided in the documentation () doesn't cover how to do it based on user input. Here's my attempt: https://codesandbox.io/s/custom-dropdown-ant-design-vue-3-2-14-fo ...

Eliminating elements from an array by analyzing the substring values

My task involves managing a list of roles stored in an array $scope.role = [];. The challenge is to display the selected role in a chip form with only the first 3 characters shown. To achieve this, I utilized substr(0,3) in my $scope.multiRoles array where ...

Utilize Vue.js methods to reverse the string within a paragraph using the appropriate function

Hello everyone, I've been attempting to implement a function to reverse a string within a paragraph text in vue.js. I've created a method called reverseword to reverse the words and added it to a card using :rule="reverseword()", but un ...

Retrieving the value of a specific property nested within a JSON object using basic JavaScript

Hey there! Thanks for taking the time to check out my question. I'm diving into JavaScript and I've hit a roadblock trying to solve this particular problem: I'm looking to extract the value of a property nested within a JSON object under a ...

Create a package that is compatible with both node and browser environments

After successfully creating a node NPM package that acts as a wrapper for a specific JSON API using node's http, https, and querystring modules, I am now looking to expand its usability by making it compatible with browsers. This would involve replaci ...

Converting bullet point list to checkboxes once requirements have been satisfied

I am working on developing a password validator with specific regex conditions in Material UI that transitions from bullet points to checkboxes when the criteria are satisfied. https://i.sstatic.net/b0pgb.png Initially, I attempted to use the npm library ...

Encountering XMLHttpRequest errors when trying to make an AJAX POST request

I'm encountering an issue with a modal containing a form for submitting emails. Despite setting up the ajax post as usual, the submission is failing consistently. The console displays the following two errors: Setting XMLHttpRequest.withCredent ...

How can you determine if multiple checkboxes have been checked with "if" statements following a button click?

I am looking to display a message after clicking a button if one or more checkboxes are checked. I would prefer using Jquery for this functionality. Any help on achieving this would be highly appreciated. $(function() { $(".btn").click(function() { ...

What is the best way to iterate through multiple arrays using a single for loop

I have various hidden popups that are triggered to show on click using some JavaScript magic. As a beginner in JavaScript, I find myself writing multiple lines of code that look very similar. Currently, my code for handling these popup boxes looks like th ...

Unable to locate desired view

I have encountered an issue while trying to develop a SPA app. Whenever I launch my application, it gets stuck in an infinite loop which eventually leads to a crash. The framework I am using is ExpressJS 4.3.0 Here is the architecture of my app: public - ...

Altering the values of nested object properties

Currently, I am struggling to update the values of keys within an array of objects. The JSON structure of the object I'm working with is quite intricate, with each object in the array potentially containing a different number of keys. Here is a simpli ...

Exploring table iteration in Angular 7

I am looking to create a table with one property per cell, but I want each row to contain 4 cells before moving on to the next row... This is what I want: <table> <tr> <td> <mat-checkbox>1</mat-checkbox& ...