在以下方法中,我需要根据项目id在第二个数组中是否存在来过滤第一个数组。由于某种原因,这对我来说还是空的。
dialogRef.afterClosed().subscribe((airlines: AirlineModel[]) => {
console.log(airlines, this.airlines);
const updates = airlines.filter(airline => this.airlines.some(item => item.id === airline.id));
console.log(updates);
});
console.log(airlines, this.airlines);
导致:
(13) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {id: "1", code: "AAL", name: "American Airlines"}
1: {id: "6", code: "DAL", name: "Delta Airlines"}
2: {id: "526", code: "SWA", name: "Southwest Airlines"}
3: {id: "27", code: "ASA", name: "Alaska Airlines"}
4: {id: "23", code: "FDX", name: "FedEx"}
5: {id: "205", code: "NCA", name: "Nippon Cargo"}
6: {id: "406", code: "UPS", name: "United Parcel Service"}
7: {id: "160", code: "CPA", name: "Cathay Pacific"}
8: {id: "403", code: "PAC", name: "Polar Air Cargo"}
9: {id: "992", code: "GTI", name: "Atlas Air"}
10: {id: "272", code: "CKS", name: "Kalitta Air"}
11: {id: "345", code: "NAC", name: "Northern Air Cargo"}
12: {id: "615", code: "BCS", name: "European Air Transport"}
length: 13
__proto__: Array(0)
(12) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {id: 6, code: "DAL", name: "Delta Airlines"}
1: {id: 526, code: "SWA", name: "Southwest Airlines"}
2: {id: 27, code: "ASA", name: "Alaska Airlines"}
3: {id: 23, code: "FDX", name: "FedEx"}
4: {id: 205, code: "NCA", name: "Nippon Cargo"}
5: {id: 406, code: "UPS", name: "United Parcel Service"}
6: {id: 160, code: "CPA", name: "Cathay Pacific"}
7: {id: 403, code: "PAC", name: "Polar Air Cargo"}
8: {id: 992, code: "GTI", name: "Atlas Air"}
9: {id: 272, code: "CKS", name: "Kalitta Air"}
10: {id: 345, code: "NAC", name: "Northern Air Cargo"}
11: {id: 615, code: "BCS", name: "European Air Transport"}
length: 12
__proto__: Array(0)
console.log(updates);
导致:
[]length: 0__proto__: Array(0)
答案 0 :(得分:4)
airlines
id
属性是一个字符串,而this.airlines
id
属性是一个数字。将比较从===
(严格)更改为==
(抽象)。
参见mdn上的Equality comparisons and sameness。
const airlines = [{"id":"1","code":"AAL","name":"American Airlines"},{"id":"6","code":"DAL","name":"Delta Airlines"},{"id":"526","code":"SWA","name":"Southwest Airlines"},{"id":"27","code":"ASA","name":"Alaska Airlines"},{"id":"23","code":"FDX","name":"FedEx"},{"id":"205","code":"NCA","name":"Nippon Cargo"},{"id":"406","code":"UPS","name":"United Parcel Service"},{"id":"160","code":"CPA","name":"Cathay Pacific"},{"id":"403","code":"PAC","name":"Polar Air Cargo"},{"id":"992","code":"GTI","name":"Atlas Air"},{"id":"272","code":"CKS","name":"Kalitta Air"},{"id":"345","code":"NAC","name":"Northern Air Cargo"},{"id":"615","code":"BCS","name":"European Air Transport"}];
const thisAirlines = [{"id":6,"code":"DAL","name":"Delta Airlines"},{"id":526,"code":"SWA","name":"Southwest Airlines"},{"id":27,"code":"ASA","name":"Alaska Airlines"},{"id":23,"code":"FDX","name":"FedEx"},{"id":205,"code":"NCA","name":"Nippon Cargo"},{"id":406,"code":"UPS","name":"United Parcel Service"},{"id":160,"code":"CPA","name":"Cathay Pacific"},{"id":403,"code":"PAC","name":"Polar Air Cargo"},{"id":992,"code":"GTI","name":"Atlas Air"},{"id":272,"code":"CKS","name":"Kalitta Air"},{"id":345,"code":"NAC","name":"Northern Air Cargo"},{"id":615,"code":"BCS","name":"European Air Transport"}];
const updates = airlines.filter(airline =>
thisAirlines.some(item => item.id == airline.id)
);
console.log(updates);
您可以使用id
-+
将字符串+airlines.id
转换为数字,或者使用-String(this.airlines.id
将数字转换为字符串,而不是使用抽象等式。 / p>
const airlines = [{"id":"1","code":"AAL","name":"American Airlines"},{"id":"6","code":"DAL","name":"Delta Airlines"},{"id":"526","code":"SWA","name":"Southwest Airlines"},{"id":"27","code":"ASA","name":"Alaska Airlines"},{"id":"23","code":"FDX","name":"FedEx"},{"id":"205","code":"NCA","name":"Nippon Cargo"},{"id":"406","code":"UPS","name":"United Parcel Service"},{"id":"160","code":"CPA","name":"Cathay Pacific"},{"id":"403","code":"PAC","name":"Polar Air Cargo"},{"id":"992","code":"GTI","name":"Atlas Air"},{"id":"272","code":"CKS","name":"Kalitta Air"},{"id":"345","code":"NAC","name":"Northern Air Cargo"},{"id":"615","code":"BCS","name":"European Air Transport"}];
const thisAirlines = [{"id":6,"code":"DAL","name":"Delta Airlines"},{"id":526,"code":"SWA","name":"Southwest Airlines"},{"id":27,"code":"ASA","name":"Alaska Airlines"},{"id":23,"code":"FDX","name":"FedEx"},{"id":205,"code":"NCA","name":"Nippon Cargo"},{"id":406,"code":"UPS","name":"United Parcel Service"},{"id":160,"code":"CPA","name":"Cathay Pacific"},{"id":403,"code":"PAC","name":"Polar Air Cargo"},{"id":992,"code":"GTI","name":"Atlas Air"},{"id":272,"code":"CKS","name":"Kalitta Air"},{"id":345,"code":"NAC","name":"Northern Air Cargo"},{"id":615,"code":"BCS","name":"European Air Transport"}];
const updates = airlines.filter(airline =>
thisAirlines.some(item => item.id === +airline.id)
);
console.log(updates);
由于您使用的是打字稿,并且要进行多次此比较,因此最好创建this.airlines
个字符串ID的Set,然后使用该字符串进行比较:
const airlines = [{"id":"1","code":"AAL","name":"American Airlines"},{"id":"6","code":"DAL","name":"Delta Airlines"},{"id":"526","code":"SWA","name":"Southwest Airlines"},{"id":"27","code":"ASA","name":"Alaska Airlines"},{"id":"23","code":"FDX","name":"FedEx"},{"id":"205","code":"NCA","name":"Nippon Cargo"},{"id":"406","code":"UPS","name":"United Parcel Service"},{"id":"160","code":"CPA","name":"Cathay Pacific"},{"id":"403","code":"PAC","name":"Polar Air Cargo"},{"id":"992","code":"GTI","name":"Atlas Air"},{"id":"272","code":"CKS","name":"Kalitta Air"},{"id":"345","code":"NAC","name":"Northern Air Cargo"},{"id":"615","code":"BCS","name":"European Air Transport"}];
const thisAirlines = [{"id":6,"code":"DAL","name":"Delta Airlines"},{"id":526,"code":"SWA","name":"Southwest Airlines"},{"id":27,"code":"ASA","name":"Alaska Airlines"},{"id":23,"code":"FDX","name":"FedEx"},{"id":205,"code":"NCA","name":"Nippon Cargo"},{"id":406,"code":"UPS","name":"United Parcel Service"},{"id":160,"code":"CPA","name":"Cathay Pacific"},{"id":403,"code":"PAC","name":"Polar Air Cargo"},{"id":992,"code":"GTI","name":"Atlas Air"},{"id":272,"code":"CKS","name":"Kalitta Air"},{"id":345,"code":"NAC","name":"Northern Air Cargo"},{"id":615,"code":"BCS","name":"European Air Transport"}];
const thisAirlinesSet = thisAirlines.reduce((s, o) => s.add(String(o.id)), new Set);
const updates = airlines.filter(airline =>
thisAirlinesSet.has(airline.id)
);
console.log(updates);
答案 1 :(得分:1)
问题是过滤器方法使用===
运算符,如果操作不正确,该运算符还会比较变量类型。两个数组的ID类型都不相同,this.airlines
的ID类型为number
, airlines
的ID类型为string
。将===
运算符替换为==
应该可以解决您的问题
答案 2 :(得分:1)
您使用的是“ ===”进行比较,因此它必须是相同类型的比较! 在这种情况下,您可以使用“ ==”来解决此问题。
答案 3 :(得分:1)
const updates = airlines.filter(airline => this.airlines.some(item => +item.id === +airline.id));
您正在使用===
,这意味着还检查了类型,因此添加一个+
符号以将其解析到int
并进行安全检查。