How to use Angular ng-repeat to filter an array by a specific key

Here is the data I am working with:

$scope.allNames = [
    "A": [ { "name": "a1" }, { "name": "a2" }, { "name": "a3"} ],
    "B": [ { "name": "b1" }, { "name": "b2" }, { "name": "b3"} ],
    "C": [ { "name": "c1" }, { "name": "c2" }, { "name": "c3"} ],
]

<div ng-repeat="(letter, names) in allNames | filter:myFilter">
    <h4>{{ letter }}</h4>
    <ul ng-repeat="n in names">
        <li>{{ n.name }}</li>
    </ul>
</div>

// when a <span> tag is clicked, filter and show only names that correspond to that letter.
<span ng-click="myFilter = 'A'></span>
<span ng-click="myFilter = 'B'></span>
<span ng-click="myFilter = 'C'></span>

I am attempting to display only the names that have the $key value of the clicked letter or start with that letter. However, I am struggling with filtering based on the array index.

Answer №1

Take a look at this link for a possible solution - http://jsfiddle.net/Shital_D/vsvxqnq7/4/

When you click on A, B, or C, the corresponding array is displayed.

Below is the provided code snippet:

 angular.module('myApp', [])
     .controller('ExampleController', ['$scope', function($scope) {
         $scope.myFilter = '';
         $scope.allNames = {
             "A": ['name1', 'name2', 'name3'],
             "B": ['name4', 'name5', 'name6'],
             "C": ['name7', 'name8', 'name9']
         };

         $scope.change = function(key) {
             $scope.myFilter = key;
         };

     }]);

HTML -

     <div ng-repeat="(key, value) in allNames" ng-show="myFilter == key">
         <h4>{{ key }}</h4>

        <ul ng-repeat="name in value">
            <li>{{ name }}</li>
        </ul>
    </div>
    <div ng-repeat="(key, value) in allNames" ng-click="change(key)">                 
        {{key}}
    </div>

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

The compatibility between Javascript and AJAX functions is currently not functioning as expected

I am attempting to send some data to the server using AJAX with the value obtained from a JavaScript variable. Here is the code snippet: <script type="text/javascript> var url; function applyPhoto(_src) { url = _src; var pho ...

AngularJS handles intricate JSON responses effortlessly

I have been learning angularjs on my own, mostly from w3schools and other websites. I have been trying to download and parse some JSON data. I was successful with a simple JSON url (http://ip.jsontest.com), but I am struggling with a more complex response. ...

Pagination in Datatables

Here is the code snippet I am working with: $('#ldap-users, #audit-users').dataTable({ "sDom": "<'row'<'span6'l><'span6'f>r>t<'row'<'span6'i><'span6'p& ...

The initial request does not include the cookie

My server.js file in the express application has the following code: var express = require('express'); var fallback = require('express-history-api-fallback'); var compress = require('compression'); var favicon = require(&apos ...

How can custom JavaScript objects have tags set upon click?

When it comes to JavaScript object referring, things can get a bit confusing. Imagine having a tag like this: <button id='myButton'>Hello</button> Now, let's say you create a custom class in JavaScript: function myClass(){ ...

Tips for modifying Colleda file vertices in A-Frame

Is it possible to update the color of a model in colleda using the code below, but how do we handle the dimensions of the vertices and update the model? For example, can we store the dimensions in a separate file (e.g. .js) and then access them in A-Fram ...

Developing a collection of customized shapes comprised of circular-edged rectangles using the Three.JS

I have been on a quest for some time now. My aim is to replicate this visual element from - the beautifully arranged deck of cards. I know it was created using Three.JS, and my goal is to recreate it exactly as seen in that link. The challenge I'm c ...

Guide to making a JavaScript button that triggers an iframe to open upon user clicking the button

I'm currently enhancing the comment section for posts on my website. I am looking to implement a button in javascript that, when clicked, will open an iframe window displaying comments similar to how Facebook does on their post. If there are other lan ...

Why does the browser keep converting my single quotation marks to double, causing issues with my JSON in the data attribute?

This task should be straightforward, but I seem to be facing a roadblock. I am trying to insert some JSON data into an input's data attribute, but the quotes in the first key are causing issues by prematurely closing the attribute. Here is the code ...

javascript: separate string into parts and substitute values

I woke up with a terrible hangover after celebrating my birthday yesterday. Can someone please provide a simple and straightforward JavaScript function to help me split a string and replace the necessary values? Keep it simple, stupid (KISS)! Here's ...

Creating an upload file input form within a VueJS Application

One of the challenges I'm facing involves creating a VueJS component with an input field for file type. Below is the code for my component: <template> <div class="flex-col justify-start w-full"> <div class="mt-2 block upper ...

Issue with AngularJS Event - DOM not updating in a timely manner

Within my application, there is an event listener that actively monitors for new messages sent to a specific user. When this event is triggered, a factory function is executed to retrieve these messages. However, it appears that the display is always one e ...

No results returned by Mongoose/MongoDB GeoJSON query

I have a Schema (Tour) which includes a GeoJSON Point type property called location. location: { type: { type: String, enum: ['Point'], required: true }, coordinates: { type: [Number], required: true ...

Is there a way to reverse the animation playback in Angular?

I am working on an animation that involves a box fading from its original color to yellow. However, I would like to achieve the opposite effect: when the page loads, I want the box to start off as yellow and then fade back to its original color. The challe ...

Retrieving the link to share for every video located in the Dropbox folder

My Dropbox folder contains a set of videos labeled "v1.avi, v2.avi, ....., vn.avi". I am looking to automate the process of extracting the share link for each video in the folder so that I can easily use it as a source value for HTML video. . . . Is ther ...

Encounter a Socket.io connection error due to net::ERR_CONNECTION_REFUSED when trying to access from multiple devices

I'm having trouble with other people's computers accessing my test chatting application. When they try to connect, they encounter this error: localhost:3000/socket.io/?EIO=4&transport=polling&t=Oi2Ko0C:1 Failed to ...

Set up webpack to exclude bundling the AngularJS library

In my AngularJS (1.5.8) application, I am utilizing webpack to bundle all the necessary libraries for the application. However, due to certain technical requirements, I do not want to include the angularjs library in the application bundle. Instead, I want ...

"Transforming a list retrieved from Django context into a JavaScript or Vue.js list: A step-by-step guide

Having a basic list presented in the django context. rlinks: ['test1', 'test2'', 'test3'] var v_root = new Vue({ delimiters: [ '[[', ']]' ], el: '#vue-main', data: { job ...

Fading text that gradually vanishes depending on the viewport width... with ellipses!

I currently have a two-item unordered list positioned absolutely to the top right of the viewport. <header id="top-bar"> <ul> <li> <a href="#">David Bowie</a> </li> <li> ...

What is the best way to access the following element of an array within a for..of loop with an if statement in Javascript?

Need help with creating a word filter that checks if index 1 is 'dog' and index 2 is 'cat' in an array. What should be checked in the next index for 'word'? let textContainer = ['bird', 'dog', 'cat& ...