gpt4 book ai didi

javascript - 比较 Angularjs 中 2 个嵌套数组之间的对象并显示相应的匹配数据

转载 作者:搜寻专家 更新时间:2023-11-01 04:31:02 25 4
gpt4 key购买 nike

我有两个数组:第一个是 empdata 保存 empid 和该员工参加的事件,然后第二个数组保存事件详细信息。

我需要将数组中的 empid 与用户输入进行比较,并使用可折叠 jQuery 显示各个员工参加的特定事件详细信息。还可以找到总价。

$scope.empdata=[];
$scope.data = [];

//first array data

$scope.empdata.push({
empid:'empid_1',
events:[{
event:'First Event'
},
{
event:'Second Event'
}
]})

$scope.empdata.push({
empid:'empid_2',
events:[{
event:'First Event'
},
{
event:'Second Event'
},
{
event:'Third Event'
}]
})

//second array data
$scope.data.push({
event:'First Event',
date: '10-jun-2015',
type:[{
name: 'Hotel Booking',
price: 400.00
},{
name: 'Flight',
price: 400.00
},{
name: 'Honorarium',
price: 900.00
}]
})


$scope.data.push({
event:'Second Event',
date: '27-july-2015',
type:[{
name: 'Hotel Booking',
price: 530.00
},{
name: 'Train',
price: 400.00
},{
name: 'Honorarium',
price: 600.00
}]
})

$scope.data.push({
event:'Third Event',
date: '20-aug-2015',

type:[{
name: 'Hotel Booking',
price: 910.00
},{
name: 'Flight',
price: 500.00
},{
name: 'Honorarium',
price: 1500.00
}]
})

提前致谢!

最佳答案

您可以为此使用自定义过滤器

Custom Filter Docs

看下面的例子

var app = angular.module('app', []);

app.filter('findobj', function() {
return function(dataobj, selected) {
return dataobj.filter(function(data) {
return (selected || []).some(function(s) {
return data.event === s.event;
});
});
};
});
app.controller('ctrl', function($scope) {

$scope.empdata = [];
$scope.data = [];
$scope.empdata.push({
empid: 'empid_1',
events: [{
event: 'First Event'
}, {
event: 'Second Event'
}]
})



$scope.empdata.push({
empid: 'empid_2',
events: [{
event: 'First Event'
}, {
event: 'Second Event'
}, {
event: 'Third Event'
}]
})

$scope.empdata.push({
empid: 'empid_3',
events: [{
event: 'Forth Event'
}, {
event: 'Fifth Event'
}]
})

//second array data
$scope.data.push({
event: 'First Event',
date: '10-jun-2015',
type: [{
name: 'Hotel Booking',
price: 400.00
}, {
name: 'Flight',
price: 400.00
}, {
name: 'Honorarium',
price: 900.00
}]
})


$scope.data.push({
event: 'Second Event',
date: '27-july-2015',
type: [{
name: 'Hotel Booking',
price: 530.00
}, {
name: 'Train',
price: 400.00
}, {
name: 'Honorarium',
price: 600.00
}]
})

$scope.data.push({
event: 'Forth Event',
date: '27-july-2015',
type: [{
name: 'Hotel Booking',
price: 530.00
}, {
name: 'Train',
price: 400.00
}, {
name: 'Honorarium',
price: 600.00
}]
})
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app">
<div ng-controller="ctrl">
<select ng-options="emp.events as emp.empid for emp in empdata" ng-model="selected"></select>
<hr/>Selected: {{selected}}
<hr/>
<div ng-repeat="d in data | findobj:selected ">{{d}}</div>
</div>
</div>

关于javascript - 比较 Angularjs 中 2 个嵌套数组之间的对象并显示相应的匹配数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30410121/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com