Sort through the angular data using an array of identifiers

I am working with a json file that contains my data

[{"id":"349","title":"event 1"},{"id":"350","title":"event 2"},{"id":"351","title":"event 3"}]

Users have the ability to save events to local storage, which are then stored in an array

$storage.myEvents = ["349","350"]

On the webpage, I display all events using the following code;

<ion-item class="item-icon-right item-brown" ui-sref="detail({id: event.id})" ng-repeat="event in events track by event.id ">
        <h2 class="positive"><b>{{event.title}}</b></h2> 
</ion-item>

I want to only show the events that the user has stored in $storage.myEvents, but my filter is not working

<ion-item class="item-icon-right item-brown" ui-sref="detail({id: event.id})"
  ng-repeat="event in events track by $index | filter: {id: $storage.myEvents }">

This results in the error

Error:  filter:notarray Expected array but received: 0
, when I check the local storage, I find

localStorage["ngStorage-myEvents"]
"["349","350"]"

Even if I change the filter to

 filter: {id: JSON.parse($storage.myEvents)}

The error persists...

How can I resolve this issue?

Answer №1

If you want to filter your events, you can create a custom filter function.

  $scope.customFilter = function (event) {
    var storage = $scope.$storage.myEvents;
    if (storage.indexOf(event.id) !== -1) return true;
    return false;
  }

  <ion-item class="item-icon-right item-brown" ui-sref="detail({id: event.id})" 
       ng-repeat="event in events | filter: customFilter track by $index">

Answer №2

looks like a pattern

event in events track by event.id

there appears to be multiple events with the same event.id (possibly some with undefined id) in the array.

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

Ways to retrieve the index of an element with a certain class

When I click on an item with a specific class, I want to retrieve its index within that class only. <div class="gallery"> <div class="gallery-item"></div> <div class="gallery-item"></div> <div class="gallery-item ...

Arranging strings in descending order using Typescript

I was attempting to arrange a string[] in a descending order. This is what I have come up with so far: let values = ["Saab", "Volvo", "BMW"]; // example values.sort(); values.reverse(); Although this method is effective, I am wondering if there is a mo ...

What steps can be taken to modify this jQuery code so that any changes made are also saved to localStorage

I have successfully used jQuery to change the background color of all my divs with the same class (userNumber1) when I check its checkbox. However, I am now looking for a way to save these changes to localStorage, so that they persist each time the page is ...

Error message "ag-grid: Unable to perform the key.forEach function in the console when resizing columns"

Within the application I am working on, I have implemented the ag-grid view. To address the issue related to the last empty pseudo column, I decided to resize the last displayed column using the 'autoSizeColumns' method of ag-grid. While this sol ...

Example of fetching Pubnub history using AngularJS

I am not a paid PubNub user. I am utilizing the example code for an Angular JS basic chat application from PubNub, and I want to access the chat history. This specific example can be found on the PubNub website. git clone https://github.com/stephenlb/an ...

Calculate the total number of elements in an integer array without relying on the Standard Template Library (STL)

I am eager to develop a program to calculate the total number of integer elements from scratch without relying on any built-in functions. I attempted the following approach: #include<iostream> using namespace std; int main(){ int arr[]={2,4,6,8}; int ...

Tips for implementing personalized command buttons in Kendo Grid with AJAX request utilizing JavaScript

I am struggling with implementing custom command buttons in a Kendo grid that makes an AJAX call using JavaScript to call a web API post action with dynamic parameters (start, stop, restart) behind button clicks. datasource dataSource = new ken ...

Struggling to synchronize the newly updated Products List array in zustand?

Let me clarify the scenario I am dealing with so you can grasp it better. I have a Cart and various Products. When a user adds the product (product_id = 1) twice to the cart with the same options (red, xl), I increase the quantity of that item. However, i ...

differences in opinion between JavaScript code and browser add-ons/extensions

As the owner and developer of an e-commerce website, I find that potential customers often contact us regarding ordering issues. Upon investigation, we consistently discover that the problem is caused by JavaScript errors. We frequently check their browse ...

Tips for changing the page URL upon click in a Vue.js application

I am currently developing a post board application using Vue.js and I am facing an issue with redirecting the page when a user clicks on each post. To handle this, I have made use of vue-route and axios in my project. Here is a snippet from index.js, ex ...

Is it possible to caution users before refreshing the page, while still allowing them to

I have a script that displays a warning message to the user when they attempt to refresh the page. It works perfectly for that purpose. However, my issue is that this same alert pops up when a user clicks on a button or a link within the website itself. I ...

How can we pass a function to a child component in Vue 2.0?

I am facing a challenge with passing a function link to the child component in Vue. Although it is functioning correctly, the code appears in HTML format. How can I enhance this? In my Vue instance, I have: app = new Vue({ ... some code data: { ...

Having problems with the functionality of AngularJS Routes

I just started learning AngularJS, and I'm having trouble with my routes. I've tried searching on Google and implementing various solutions, but nothing seems to be working. Here is a snippet from my index.html file located in public/index.html: ...

arrange a collection within an array containing keys as strings

I am facing an issue with sorting an array of objects. I need to sort the 'list' by 'score' in descending order. var list =[{ '440684023463804938': { score: 6, bonuscount: 2 }, '533932209300832266': { score: 20, b ...

Creating a stand-alone NPM module for a Redux store to be used in a React application

Can the Redux Store be developed as a separate npm package for a React App and then imported into it? For instance: Assuming there is a fictional React Project named reactapp. A package called reactapp-reduxstore is created containing the Redux Store, al ...

Combining column headers in ng-table with AngularJS

Trying to merge columns with ng-table I've been attempting to merge table headers with ng-table, After searching through ng-table's documentation, I couldn't find anything, I came across the html attribute 'columnspan', but my g ...

What is the most effective way to extract non-zero values from a text file?

In a text file, I have data of different values. 15 1 23 0 39 -1 71 -1 79 1 95 1 127 -2 151 2 183 1 191 -1 239 0 247 3 To convert this data into a 2d list, I used the following code and obtained the result below: [[15, 1.3 ...

Having issues with Angular chart.js onClick event? Learn how to retrieve all elements of a bar when it is clicked on

As a newcomer to Chart.js, I am exploring how to implement click events for when a chart is generated. My current challenge involves accessing all the elements of a bar upon clicking it. Unfortunately, the onClick method is not functioning as expected at ...

Accessing a JavaScript variable in another <script> section

I'm facing a challenge where I need to access a JavaScript variable that is declared in one block of an HTML page from another block on the same page. This is crucial for me to be able to halt an AJAX call that is currently ongoing. However, I'm ...

What is preventing the table from extending to the full 100% width?

Displayed above is an image showing an accordion on the left side and content within a table on the right side. I have a concern regarding the width of the content part (right side) as to why the table is not occupying 100% width while the heading at the ...