Angular如何使用点击功能提取和显示特定国家名称的JSON数据。

huangapple go评论87阅读模式
英文:

Angular how to extract and display specific JSON data based on country name using click function

问题

我有以下JSON格式的数据。

const period: Data[] = [
{
  name: "Germany",
  id: 1,
  data: [
    { date0: "2015-01-28", date1: "2015-02-04" },
    { date0: "2015-08-27", date1: "2015-09-07" },
    { date0: "2015-12-24", date1: "2016-01-05" }
},
{
  name: "France",
  id: 2,
  data: [
    { date0: "2015-01-28", date1: "2015-02-04" },
    { date0: "2015-08-27", date1: "2015-09-07" },
    { date0: "2015-12-24", date1: "2016-01-05" }
}];

我可以在我的方法中调用服务来获取所有这些数据。

this.admissionService.getAdmissions().subscribe(perioddata =>
{
  this.admission = [perioddata]
)};

但我只想根据国家名称调用数据,通过实现onValueChanged($event)方法。

onValueChanged(data) {
  this.admission.filter(['country', '=', data.value]);
}

基本上,我的前端是这样的,我从列表中选择一个国家,它应该根据国家名称调用数据。

英文:

I have JSON format data as below.

 const period: Data[] = [
 {
   name: "Germany",
   id: 1,
   data: [
     { date0: "2015-01-28", date1: "2015-02-04" },
     { date0: "2015-08-27", date1: "2015-09-07" },
     { date0: "2015-12-24", date1: "2016-01-05" }
},
{
   name: "France",
   id: 2,
   data: [
     { date0: "2015-01-28", date1: "2015-02-04" },
     { date0: "2015-08-27", date1: "2015-09-07" },
     { date0: "2015-12-24", date1: "2016-01-05" }

I can call the service in my method to get all of this data.

   {
    this.admissionService.getAdmissions().subscribe(perioddata =>
      {
    this.admission = [perioddata]
    
   )};

However i only want call data based on name of the country by implementing onValueChanged($event)

 onValueChanged(data) {
    this.admission.filter(['country', '=', data.value]);
}

<dx-select-box
            id="selectbox"
            displayExpr="name"
            (onValueChanged)="onValueChanged($event)"
        ></dx-select-box>

Basically my front end is that, i select a country from the list and it should call data based on country name.

答案1

得分: 2

如果您的onValueChanged方法的data是某个国家,那么您可以过滤您的period数组:

onValueChanged(data) {
    this.period.filter(f => f.name == data);
}

一个示例:

const period = [
    {
        name: "Germany",
        id: 1,
        data: [
            { date0: "2015-01-28", date1: "2015-02-04" },
            { date0: "2015-08-27", date1: "2015-09-07" },
            { date0: "2015-12-24", date1: "2016-01-05" }
        ]
    },
    {
        name: "France",
        id: 2,
        data: [
            { date0: "2015-01-28", date1: "2015-02-04" },
            { date0: "2015-08-27", date1: "2015-09-07" },
            { date0: "2015-12-24", date1: "2016-01-05" }
        ]
    }
];

event = 'France';
const result = period.filter(f => f.name == event);
console.log(result);
英文:

If your data of method onValueChanged is some country, then you can filter your array of period:

onValueChanged(data) {
    this.period.filter(f => f.name == data);
}

An example:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const period = [
    {
        name: &quot;Germany&quot;,
        id: 1,
        data: [
            { date0: &quot;2015-01-28&quot;, date1: &quot;2015-02-04&quot; },
            { date0: &quot;2015-08-27&quot;, date1: &quot;2015-09-07&quot; },
            { date0: &quot;2015-12-24&quot;, date1: &quot;2016-01-05&quot; }
        ]
    },
    {
        name: &quot;France&quot;,
        id: 2,
        data: [
            { date0: &quot;2015-01-28&quot;, date1: &quot;2015-02-04&quot; },
            { date0: &quot;2015-08-27&quot;, date1: &quot;2015-09-07&quot; },
            { date0: &quot;2015-12-24&quot;, date1: &quot;2016-01-05&quot; }
        ]
    }
];

event = &#39;France&#39;;
const result = period.filter(f =&gt; f.name == event);
console.log(result);

<!-- end snippet -->

答案2

得分: 0

onValueChanged(event) {
   const country = event.target.value;
   this.admissionService.getAdmissions().pipe(
    // 获取相关对象(假设是德国部分)
     map(countriesData => countriesData.find(countryData => countryData.name === 
          country)),
    // 从相关对象中获取数据数组
    map(countryData => countryData.data),
   ).subscribe(dataForCountry => {
     // 在此处进行必要的操作,我现在只是记录它。
     console.log(dataForCountry);
  });
}

如果您使用较旧版本的 RxJs,则应为:

this.admissionService.getAdmissions()
    .map(countriesData => countriesData.find(countryData => countryData.name === country))
    .map(countryData => countryData.data)
    .subscribe(dataArrayForCountry => {
      console.log(dataArrayForCountry);
  });
英文:

Can you do:

onValueChanged(event) {
   const country = event.target.value;
   this.admissionService.getAdmissions().pipe(
    // get the object in concern (let&#39;s say the Germany slice)
     map(countriesData =&gt; countriesData.find(countryData =&gt; countryData.name === 
          country)),
    // get the data array from the object in concern
    map(countryData =&gt; countryData.data),
   ).subscribe(dataArrayForCountry =&gt; {
     // do whatever you need, at this point I am just logging it.
     console.log(dataForCountry);
  });
}

If you're using an older version of RxJs, it should be:

this.admissionService.getAdmissions()
    .map(countriesData =&gt; countriesData.find(countryData =&gt; countryData.name === country))
    .map(countryData =&gt; countryData.data)
    .subscribe(dataArrayForCountry =&gt; {
      console.log(dataArrayForCountry);
  });

huangapple
  • 本文由 发表于 2020年1月3日 19:53:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/59578160.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定