What is the best way to extract specific data from a JSON file based on a chosen property using AngularJS and the $http.get method?

I am working with a JSON file that contains color information like this:

{
  "colorsArray":[{
    "colorName":"red",
    "hexValue":"#f00"
  },
  {
    "colorName":"green",
    "hexValue":"#0f0"
  },
  {
    "colorName":"blue",
    "hexValue":"#00f"
  },
  {
    "colorName":"cyan",
    "hexValue":"#0ff"
  },
  {
    "colorName":"magenta",
    "hexValue":"#f0f"
  },
  {
    "colorName":"yellow",
    "hexValue":"#ff0"
  },
  {
    "colorName":"black",
    "hexValue":"#000"
  }
]}

In my JavaScript file, I have the code snippet below where I use $http.get to fetch the JSON data:

var app = angular.module('app', ['ngTagsInput']);
app.controller('MainCtrl', function($scope, $http) {

  $scope.loadTags = function(query) {
      return $http.get('tags.json');
  };
});

I want to extract only the colorName values from the JSON file when using AngularJS and making an external call with $http.get. How can I achieve this?

Answer №1

When a request is made to the json file, it is not possible to query the file directly. The most efficient way to retrieve specific data is by sending an HTTP call with query parameters to a backend service method, which will then execute and return the desired result from the server side.

Alternatively, another approach is to filter the data on the client side. In this scenario, all JSON data is initially returned as the response, and then the relevant information needs to be filtered out within the controller.

$scope.loadTags = function(query) {
    return $http.get('tags.jon').then(function(response) {
        var data = response.data.map(function(obj) {
            return obj.colorName;
        })
        return data;
    })
};

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 display the data within a BLOB object

On this page, the user is showcasing a table with three columns - tipo_esame (string), data_esame (string), and uri (BLOB). const archiveItems = this.state.archive.map((archive, i) => { return ( <tr key={archive.hash_referral}> <td ...

Tips for transferring an item to PHP

I am facing a challenge in sending data from the client side (javascript) to the server (php) using ajax. My object structure is as follows: sinfo={ topic_title:title, topic_id: tid, section_name:section_name, ...

Reactjs may have an undefined value for Object

I have already searched for the solution to this question on stackoverflow, but I am still confused. I tried using the same answer they provided but I am still getting an error. Can someone please help me resolve this issue? Thank you. const typeValue = [ ...

Leveraging a JSON file as a data repository for chart.js

I am struggling to incorporate JSON values into a bar chart. I have successfully logged the JSON data in the console, but I'm unsure how to include it in the data property for the chart. Below is the source JSON... {time: "2016-07-03T21:29:57.987Z" ...

A step-by-step guide on integrating vuetify-component into codeceptjs

When attempting to write tests using codecept.js, I am facing difficulties accessing the vuetify components. <v-layout> <v-flex xs7> <v-text-field ref="video1min" v-model="video1min" :rules=" ...

Execute a function with parameters when a button is clicked dynamically using PHP

I am trying to execute a parameterised function in PHP using AJAX. Below is the code snippet of my dynamic button where I need to pass $sub_id to the delet(sub_id) function for executing some SQL: echo "<td><input type='submit' name=&a ...

Constructing a new mongoose request without nesting by sending multiple requests

Currently, I am working on an application where I receive a POST request with two variables. I then extract information from three collections based on these variables and use the collected data to make a save request to another collection. The structure o ...

Stop processing the current websocket connection once a new websocket request is received

Currently, I am utilizing the npm ws module (or its wrapper named isomorphic-ws) for establishing a websocket connection. NPM Module: isomorphic-ws This setup allows me to retrieve an array of data from a websocket++ server situated on the same local mac ...

Creating a fresh array by applying a filter and assigning keys

I am facing an issue with my array structure, it looks like this, [ [ "Show/Hide", "P000", "MAX-CT05 FVM2-", "S", 1532, -9.5929406005, null, null, ...

Troubleshooting CSS Animation Failure in Internet Explorer 11

My mouse over / mouse out animation works perfectly in Firefox and Chrome, but it's not functioning in IE. Can anyone suggest why this might be happening when it was working fine before? var actual = 1; var over = 0; var over2 = 0; function scrol ...

Common issues encountered when using the app.get() function in Node.js

I've been attempting to develop a website that utilizes mongojs. I've implemented the code snippet below, but when I launch the site, it never reaches the app.get() section, resulting in a 500 error on the site. How can I ensure it responds to th ...

Pre-selected default value in select box using ng-options

I'm working on setting a default value in my select box, retrieved from the database, using ng-options. Here's my view: <select class="form-control samlength modalinput" ng-options="p.procid as p.procname for p in proce ...

Ways to unmark the "select all" checkbox when any one of its children is no longer selected

Is there a way to automatically uncheck the "Select All" checkbox when any of its child checkboxes are deselected? In the code snippet provided, the goal is for the "Select All" checkbox to be unchecked if not all child checkboxes are selected. The script ...

Validation of forms using Javascript

I currently have an HTML form with JavaScript validation. Instead of displaying error messages in a popup using the alert command, how can I show them next to the text boxes? Here is my current code: if (document.gfiDownloadForm.txtFirstName.value == &ap ...

What mistakes am I making in this PHP code as I try to work with the select option?

Currently, I am developing a form that involves selecting values. If the user chooses 'yes', I want to display a text box section. However, the code below is not functioning as expected. <select id="gap" name="gap" onclick="gap_textbox();"> ...

Tips for triggering a sound only when transitioning from a true to false value for the first time

I have data for individuals that includes a dynamically changing boolean value. This value can be true or false, and it updates automatically. The webpage fetches the data every 5 seconds and displays it. If the value for any person is false, a sound is p ...

Removing White Spaces in a String Using JavaScript Manually

I have created my own algorithm to achieve the same outcome as this function: var string= string.split(' ').join(''); For example, if I input the String: Hello how are you, it should become Hellohowareyou My approach avoids using ...

AngularJS allows you to toggle the visibility of a div at set intervals, creating

I am struggling with the task of showing and hiding a div that serves as an alert for my application. Currently, I am using $interval to create a continuous show and hide action on the div. However, what I aim for is to have the DIV visible for X amount o ...

Can jQuery effortlessly glide downward, come to a stop, continue downward, and then move upwards?

My webpage features a large table created and populated automatically every minute using ajax. The code structure is as follows: $(document).ready(function(){ setInterval(function(){ $.ajax({ //code to call backend, get the data, ...

One method for assigning a unique identifier to dynamically generated buttons with jQuery

<table border="0" class="tableDemo bordered"> <tr class="ajaxTitle"> <th width="2%">Sr</th> <th >SM</th> <th >Campaign</th> <th >Day1</th> <th >Da ...