英文:
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: "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);
<!-- 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's say the Germany slice)
map(countriesData => countriesData.find(countryData => countryData.name ===
country)),
// get the data array from the object in concern
map(countryData => countryData.data),
).subscribe(dataArrayForCountry => {
// 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 => countriesData.find(countryData => countryData.name === country))
.map(countryData => countryData.data)
.subscribe(dataArrayForCountry => {
console.log(dataArrayForCountry);
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论