How to efficiently filter objects within a child array in an ng-repeat statement, utilizing track by $index to manage duplicates

I have a variable called narrow.searchedMenu that contains 3 arrays of equal length. To display them, I am using ng-repeat with track by $index due to some duplicate elements present.

    <ul>
      <li ng-repeat="item in narrow.searchedMenu.description track by $index">
         {{ narrow.searchedMenu.name[$index] }},
         {{ narrow.searchedMenu.short_name[$index] }},
         {{ narrow.searchedMenu.description[$index] }}</li>
  </ul>

My goal is to filter the displayed results based on keywords appearing in the description. If a description does not match, I want to exclude the corresponding name, short_name, and description.

In normal circumstances, I would use something like this:

| filter{description:'chicken'} 

However, when I try to add it to the ng-repeat statement along with track by, it throws an error:

Error: [$parse:syntax] http://errors.angularjs.org/1.5.8/$parse/syntax?p0=%7B&p1=is%20an%20unexpected%20token&p2=16&p3=NaNndex%20%7C%filter%7Bdescription%3A'chicken'%7D&p4=%7Bdescription%3A'chicken'%7D

I've tried different approaches but haven't had any success so far.

It's worth mentioning that the $scope isn't injected into my controller. When attempting to use a custom filter, I encounter the following error:

"Error: [filter:notarray] errors.angularjs.org/1.5.8/filter/notarray?p0=0";

Lastly, I've been advised against using includes due to lack of broad support. There was mention of using a polyfill as an alternative solution, though I'm unsure if it applies in this case.

Answer №1

If you want to apply a custom filter to an object in your controller, make sure to include track by as the last statement in your ng-repeat directive.

An example of a filter in your controller could be:

menu.chickenFilter = function(item) {
  return (item.indexOf('chicken') > -1)
}

Adjust your ng-repeat like this:

<li ng-repeat="item in narrow.searchedMenu.description | filter: narrow.chickenFilter track by $index">
         {{ narrow.searchedMenu.name[$index] }},
         {{ narrow.searchedMenu.short_name[$index] }},
         {{ narrow.searchedMenu.description[$index] }}</li>

Answer №2

Try out this solution:


<ul>
  <li ng-repeat="item in narrow.searchedMenu track by $index" ng-if="narrow.searchedMenu[$index].description.indexOf('chicken')>-1">
     {{ narrow.searchedMenu.name[$index] }},
     {{ narrow.searchedMenu.short_name[$index] }},
     {{ narrow.searchedMenu.description[$index] }}</li>
</ul>

You can view the plunker example I created here:

http://plnkr.co/edit/KXvD4JgwRe9wNszt8rCy?p=preview

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

divide a JSON list

Within this JSON array, there is a plethora of person data, each with their own specified type such as "hr" or "accounting". The structure of the JSON array is depicted below: { "0": { "id": "2", "name": "blabla", "type": "hr" ...

This marks my initial attempt at developing an Angular project using Git Bash, and the outcome is quite remarkable

I have a project named a4app that I am trying to create, but it seems to be taking around 10 minutes to finish and is showing errors. The messages displayed are quite odd, and I suspect there may be an issue with the setup. I have not yet used the app that ...

Ways to resolve axios promise

My current task involves working on a GET request from an API. Upon receiving the response shown in the image, I noticed that the data presented in the promise is accurate. My goal is to display the letters and their corresponding promise data within the H ...

"Make a phone call following the completion of an HTTP

Upon receiving and storing data, I am looking to execute a function but the code I currently have is not producing the desired result. Could someone provide some insight into what I might be doing incorrectly? Here's my current code snippet: $h ...

Is it possible to utilize ANGULAR1 modules within ANGULAR2?

After completing numerous projects in AngularJS, I've noticed that the world is rapidly progressing towards HTML5 and the latest JavaScript engine technologies. The Angular team is now heavily focused on the development of Angular2, although there ar ...

how can I locate and interact with nested elements using WebDriver in Java

In the HTML code provided below, I am trying to click on the cssselector (span.icon_edit ClsUpdate) within the span tag. <div class="final_textarea"> <div class="tab_lable_right"> <textarea rows="2" cols="50" id="txttab_2" readonly= ...

Unveiling the approach to accessing a nested function with jQuery

While the title may be a bit misleading, I couldn't think of a better way to describe it. I've created a function that allows a small pop-up window to appear when a link is clicked (to confirm whether or not an article should be deleted). Addit ...

Tips for adjusting the width of a Facebook embedded video within its container using ReactPlayer

Has anyone encountered issues with adding an embedded video to a NextJS app using ReactPlayer and Facebook videos? It seems that Facebook does not support the width="100%" attribute, as it always snaps back to 500px. Interestingly, this works wel ...

JavaScript promise will return a value that is undefined

Trying to pass a jQuery ajax request through a promise like this: var foo; foo = bar().then(function(response) { console.log("Success!", response); console.log(foo[0].bool); }, function(error) { console.error("Failed!", error); }); console.lo ...

Is using debounce with $scope.$apply a logical choice?

In my experience, I have come across a method that claims to decrease the number of $digest loops by incorporating debouncing into the $scope.$apply process. It looks something like this: $scope.$apply = _.debounce($scope.$apply, 250); Is this approach v ...

Ways to reach component method via router

When making an Ajax request and displaying the data in a table component, I encounter an issue where I need to extract the clicked data for use in another Ajax request within a different component that is called through React-Router. The challenge lies in ...

When the @change event is triggered, Vue data objects do not exhibit reactivity

Trying to figure out why the msg and show data parameters are not updating when triggered by @change. To ensure that these parameters are only updated upon successful file upload, I placed them inside the lambda function of onload: reader.onload = functio ...

A different approach to calling JavaScript functions

I have a method that populates an array. I would like to utilize it in this manner: arrayname.fill("First Array"); And not like this: arrayname = fill("First Array"); What approach should I take? function fillArray(name) { let newArray = ...

Incorporate a personalized array into Google Autocomplete Places using AngularJS

I'm working on my application and I've implemented autocomplete for Google Places using the gm-places-autocomplete directive. However, I would like to enhance this autocomplete feature by incorporating my custom array of locations along with the ...

JqueryUI Autocomplete functions flawlessly on JSFiddle but fails to work on the actual website

After spending hours working on it, I still can't seem to figure it out. The code functions perfectly on JSFiddle, but not on my own website. Here is the link to the JSFiddle: http://jsfiddle.net/x69chen/sbAR6/16/ I've also included the links ...

React Component not retaining its value

I am currently facing an issue with a React component where I need to read a file from an Upload and pass the contents onto a child component for display. Although I can successfully read the file contents using FileReader and see the results in the cons ...

ReactJS Chatkit has not been initialized

I made some progress on a tutorial for creating an Instant Messenger application using React and Chatkit. The tutorial can be found in the link below: https://www.youtube.com/watch?v=6vcIW0CO07k However, I hit a roadblock around the 19-minute mark. In t ...

Stop the Bootstrap accordion from collapsing when clicking on an internal anchor (href)

Is there a way to prevent the accordion from collapsing when opening a new page after clicking on a link? I have included the HTML code below, but I need assistance with the JavaScript to achieve this. I'm facing an issue where the accordion category ...

What is the reason for Jquery AJAX appending additional path to the relative path?

I am encountering an issue with the following code snippet $.ajax({ url: "search/prefetch", success: function (data) { $(".result").html(data); alert("Load was performed."); }, dataType: "json" } ...

What is the technique for performing asynchronous querying of multiple SQL databases?

Currently, I am in the process of developing a web application using nestjs and typeorm. I have been contemplating the functionality of the following code: const r1 = await this.connection.query(sqlA) const r2 = await this.connection query(sqlB) Does th ...