Navigating an object using a key/value pair mapping technique

After receiving data from the API, I am faced with an object structured like this:

          response =  {
                        "0": [
                                    {
                                        "description": "basket",
                                        "color": "red"
                                    },
                                    {
                                        "description": "pillow",
                                        "color": "blue"
                                    },
                                    {
                                        "description": "armchair",
                                        "color": "white"
                                    }
                               ],
                         "1" : [
                                        {
                                            "description": "shirt",
                                            "color": "black"
                                        },
                                        {
                                            "description": "pants",
                                            "color": "blue"
                                        }
                                ]
                        }

My goal is to showcase this data in my view: https://i.sstatic.net/vaTjk.png

// controller.js

self.catKeys= Object.keys(response)
self.catValue= Object.values(response)

// view.html

  <div ng-repeat="cat in ctrl.catKeys" class="col-md-3"> {{ cat }} </div>

  <div ng-repeat="val in ctrl.catValue" class="col-md-9">
    {{ val[index].description }} {{ val[index].color}}
  </div>

I've experimented with various methods, but still struggling to make it work..please help me out :(

Answer №1

To combine each item with its corresponding color, you can utilize the Array#map method and then concatenate them into a single string using join.

for (const [key, value] of Object.entries(response)) {
  response[key] = value.map(item => item.color + ' ' + item.description).join(' - ');
}
self.response = response;

After that, displaying the results is straightforward:

<div ng-repeat="(key, value) in ctrl.response" class="row">
    <div class="col-md-3">{{key}}</div>
    <div class="col-md-9">{{value}}</div>
</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

establish expiration date for image

On my e-commerce site, users upload images to customize product items, such as adding their photo to a cake. I'm curious if there's a way to automatically delete these images after a certain expiry date. Is it possible to implement an automatic ...

Using jQuery, you can create and add an element to the page when a user clicks

Currently, my webpage contains numerous anchor tags that link to various video files, along with a single div element: <a href="http://www.example.com/example-1.mkv" class="video">1</a> <a href="http://www.example.com/example-2.mkv" class=" ...

Maintain text while transitioning to another page

On my webpage (refer to image for details), users need to select options through a series of steps. The process involves clicking on a link in the top box, then entering a search term in the searchbox to display relevant links in div1. Clicking on a link ...

Exploring the process of setting up reactive forms with AngularJS to handle arrays

I am working on setting up a reactive form for AngularJS and need to initialize certain fields: initializeBusinessEposForm(): void { this.businessEposForm = this.formBuilder.group({ custom_pos_priority: new FormControl(false), custom_float_ ...

What is the best approach for assigning initial values to data retrieved from Vuex?

My objective is to develop an 'edit account' form where users can update their account details. I aim to display the account information in a pre-filled form that includes fields like username, email, and address. Users will be able to make chan ...

Organizing month and year in angularjs: A step-by-step guide

I have a table with a column for month and year. I need to sort the dates by year. Can someone please help me with this? You can view the code in the provided fiddle. In my case, the date format is "MMMM yyyy". For example, if I have "August 2016", "Septe ...

Top method to incorporate reusable SVG elements in React applications

I have implemented SVG as a reusable component in my React application. Here is a sample of the SVG component: import React from 'react'; export default function IconComponent(): JSX.Element { const svg = ` <svg width="40" h ...

Assign authorization settings to a document when generating a new spreadsheet using the Google API

Using the Google Spreadsheets API in NodeJS, I successfully created a Spreadsheet with the code snippet below: const auth = new GoogleAuth({ keyFile: 'src/credentials.json', scopes: "https://www.googleapis.com/auth/spreadsheets" ...

What could be the reason why a specific item is not being retrieved by its ID?

Whenever I navigate to the route: '/gallery', all the items from the file './posts.json' are fetched and displayed on the screen. However, upon navigating to the route: '/gallery/:id', my intention is to retrieve a single item ...

Repeated calls to Angular's <img [src]="getImg()"> frequently occur

Can someone help me figure out what's going on here? Recently, I set up a new Angular project and in app.component.html, I have the following code: <img [src]="getDemoImg()"> In app.component.ts: getDemoImg(){ console.log('why so m ...

Using dojox gfx to enable a node to be draggable following retrieval of sgv data from a JSON source

I have a collection of shapes on a surface that I need to convert to JSON using the dojox.gfx.utils.toJson(surface) method. Once I have the JSON data, I then utilize dojox.gfx.utils.fromJson(surface, json) to append it back to the surface. However, I enco ...

React conditional statement within a map function embedded in a JSX element

Below is the function I have created to generate tabbed items: function ConstructTabs(props) { const tabs = props.tabs; const makeTabs = tabs.map((tab, index) => { <React.Fragment> <input className="input-tabs" id ...

Best practices for effectively dismantling a Paper.js Scope

In my web project, I am utilizing multiple Paper.js canvases by creating a new scope for each of them. Due to the AJAX-driven nature of the site, I need to get rid of unnecessary instances when switching between subpages. Unfortunately, there is no built-i ...

Using Javascript to Retrieve "Images" from the "Resources" Directory

I typically use Google Chrome as my browser of choice. Unfortunately, the images on certain websites are causing lagging issues. I am looking for a solution to access and clean up these problematic images in order to improve browsing performance. ...

Creating markers from Mysql database is a simple and efficient process

On my website, I have an array of markers that I use to display locations on a Google map. The array format I currently use is: generateMarkers([['Location', lat, long], ['Location2', lat2, long2],['Location3', lat3, long]3]) ...

I'm experiencing issues with the control.reset() function in the trackball controls JS not functioning properly

Visit this website www.naamdesigns.com/arv Whenever a link is clicked, I want the sphere to rotate back to its original position. I have managed to reset the camera position successfully, but I'm struggling to tween the reset rotation. Any tips on ho ...

The AppScan findings have identified a vulnerability related to Client Cross Frame Scripting Attack

After running AppScan on my codebase, it flagged a medium severity issue of Client Cross Frame Scripting Attack. Below is the snippet of the code in question: /index.html Line no: 1 <!DOCTYPE html> Do you have any ideas on how to address this issu ...

Access the current page's URL using JavaScript

I have a unique URL structure that looks like this: index.html#page:page.php As I am loading my pages dynamically using AJAX, I want to create hotlinks to load specific pages. Currently, I am using the following function: function getdata(file){ $ ...

Obtaining binary data directly from a combination of buffers

When working with the nodeJS Buffer().toString('utf8'), I encountered the following: Content-Type: audio/x-ms-wma Content-Disposition: form-data; name=recordedAudio \r\n\r\n0&uf\u0011\u0000 My goal is to strip ...

What is the best way to implement a calendar view for selecting time periods on an HTML page?

Is there a way to implement a calendar view when clicking on the input box within an HTML page? I am looking to create a function where users can select a time period to generate a file. How can this be accomplished? If possible, could you share some samp ...