简体   繁体   English

在Angular 2中将对象存储在数组中的对象内部

[英]Storing Objects inside Object in Arrays in Angular 2

I'm trying to store this data, given from a Wordpress Backend with HTTP Get Request in Ionic 2 (Angular 2). 我正在尝试存储此数据,该数据是通过Ionic 2(角度2)中具有HTTP Get Request的Wordpress后端提供的。

I'm receiving this data structure, 我正在接收此数据结构,

Console Log of data response- 控制台数据响应日志-

在此处输入图片说明

I'm trying to store this data like the menus (menu_1 and menu_2) in array of menus, the categories in array of categories, dishes in array of dishes... 我正在尝试将这些数据(如菜单(menu_1和menu_2))存储在菜单数组中,将类别存储在类别数组中,将菜式存储在菜肴中...

How can I do that? 我怎样才能做到这一点?

I don't want to show or iterate using Pipes, I only want to storage in Arrays to work easier with them. 我不想显示或使用Pipes进行迭代,我只想存储在Array中以便更轻松地使用它们。

My code at the moment is like: 目前,我的代码如下:

home.ts: home.ts:

I have a injectable class (Globals) to call the http get, but I do the subscribe in the getMenus function on my home.ts component: 我有一个可注入类(全局变量)来调用http get,但是我在home.ts组件的getMenus函数中进行了订阅:

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Globals } from '../../providers/globals';

@Component({
  selector: 'page-home',
  providers: [Globals],
  templateUrl: 'home.html'
})
export class HomePage {
  menus: any;

  constructor(public navCtrl: NavController, public globals: Globals) {
    this.getMenus();
  }

  getMenus() {
    this.globals.getMenus().subscribe(
      data => {
        console.log(data);
        this.menus = data;
      },
      err => { console.log(err) }
    );
  }
}

And I have created a class, called Menu, at the moment is very simple: 我创建了一个名为Menu的类,此刻非常简单:

import { Injectable } from '@angular/core';
import 'rxjs/add/operator/map';

@Injectable()
export class Menu {
  name: any;
  categories: any;

  constructor() {
    this.name = this.name;
    this.categories = this.categories;
  }
}

Where name is basic field of the object (key: name, value: "Today's menu" and categories is cat_1, cat_2 (two objects inside menu_1 object, which each contains more objects (dish_1, dish_2...). 其中名称是对象的基本字段(键:名称,值:“今天的菜单”,类别是cat_1,cat_2(menu_1对象内的两个对象,每个对象包含更多对象)(dish_1,dish_2 ...)。

My idea is create a class for every one of them, class Menu, class Category and class Dish. 我的想法是为他们每个人创建一个班级,班级Menu,班级Category和班级Dish。 But I have any idea of how can I start store this objects in this classes. 但是我对如何开始将这些对象存储在此类中有任何想法。 :S :S

Greetings! 问候!

What are you trying to do ? 你想做什么 ?

I think what you're trying to do is to normalize your data. 我认为您正在尝试做的是规范化数据。

(Are you using a Redux pattern ? Maybe Ngrx ? If so, this is a great idea to normalize !) (您是否正在使用Redux模式?也许是Ngrx?如果这样,将其标准化是个好主意!)

Here's how a normalized state looks like : http://redux.js.org/docs/recipes/reducers/NormalizingStateShape.html 标准化状态如下: http : //redux.js.org/docs/recipes/reducers/NormalizingStateShape.html

How should you do it ? 你应该怎么做?

You can either do it by hand, which will become quite hard if you have many other requests to deal with, or you can describe your data in schema and use normalizr to do this job (normalizing data) for you. 您可以手动执行此操作,如果要处理许多其他请求,这将变得非常困难,或者可以在架构中描述数据并使用normalizr替您完成此工作(对数据进行标准化)。

If you don't know where to start. 如果您不知道从哪里开始。 You can try this approach. 您可以尝试这种方法。 First, create a model: 首先,创建一个模型:

export class DummyModel {
 menu: any;
 cat: any;
 dish: any;
 ...
//you can replace any with the type expected (string, number, etc)
}

In your component, you import your dummyModel and you set the data 在您的组件中,导入您的dummyModel并设置数据

import { DummyModel }                from '../dummy.model';
/...
dummyModel: DummyModel = dummyData;

Also, consider @Nitzan Tomer advise, try to write your code and people here can help if you are facing an issue 另外,请考虑@Nitzan Tomer的建议,尝试编写代码,如果您遇到问题,这里的人员可以提供帮助

The first thing to do is to create an interface for the data that you receive from the server, something like: 要做的第一件事是为从服务器接收的数据创建一个接口,例如:

interface Dish {
      Name: string;
      Description: string;
      Thumbnail: string;
}

interface Category {
      [name: string]: Dish;
}

type ServerResponse = {
      [name: string]: { [name: string]: Category; } & { name: string };
}

If you want to create classes from this data you can then: 如果要根据这些数据创建类,则可以:

class Menu {
      name: string;
      categories: { [name: string]: Category };

      constructor(data: { [name: string]: Category; } & { name: string }) {
            this.name = data.name;
            this.categories = {};

            Object.keys(data).forEach(name => {
                  if (name !== "name") {
                        this.categories[name] = new Category(data[name]);
                  }
            });
      }
}

(data: ServerResponse) => {
      this.menus = {};
      Object.keys(data).forEach(name => {
            this.menus[name] = new Menu(data[name]);
      });
}

You should also create the Category class and all, but that's the idea. 您还应该创建Category类和所有类,但这就是想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM