[英]Filtering an object to check wheter all the elements of an array is present or not
I am new in concept of key-value pair and trying to figure out a way to filter all the key value pairs which has all the elements of an array, but I am struck somewhere inside the looping. 我是键值对概念的新手,试图找出一种方法来过滤具有数组所有元素的所有键值对,但是我在循环内部感到震惊。
my key value pair: 我的关键值对:
Alltrips = {
"20180301": [{
"transporter": {
"company": "Pradeep Transport",
"id": 1518419163555
},
"vehicleRegistrationNumber": "AS01FF1234",
"destinations": [{
"Id": "pcwYK",
"v": "Bhubaneswar, OD"
}, {
"Id": "fy3yF",
"v": "Tiruppur, TN"
}],
"Owner": "Gogs"
},
{
"transporter": {
"company": "tes Transport",
"id": 1518419163515
},
"vehicleRegistrationNumber": "AS01FF1454",
"destinations": [{
"Id": "coWoz",
"v": "Vizag, AP"
}, {
"Id": "EZuh7",
v: "Ambala Sadar, HR"
}],
"Owner": "Acme"
}
],
"20180406": [{
"transporter": {
"company": "tes Transport",
"id": 15184195663555
},
"vehicleRegistrationNumber": "AS01FF1444",
"destinations": [{
"Id": "pcqYK",
"v": "Coorg"
}, {
"Id": "fy3yF",
"v": "Tiruppur, TN"
}],
"Owner": "pyu"
}
],
"20180412": [{
"transporter": {
"company": "tes Transport",
"id": 15184195663555
},
"vehicleRegistrationNumber": "AS01FF1144",
"destinations": [{
"Id": "pcqYK",
"v": "Coorg"
}, {
"Id": "fy3yF",
"v": "Tiruppur, TN"
}],
"Owner": "ps"
}
]
}
and I have an array: 我有一个数组:
searchOptions=
[{label: "tes cargo", value: "tes cargo", type: "TransporterType"}, {label: "AS01FF1444", value: "AS01FF1444", type: "VehicleType"},{label: "Coorg State, Kodagu Dist., Karnataka", value: "Coorg State, KA", type: "PlaceType"}]
I have to return an resultant object " final " which contain all the " Alltrips " key value pair containing the searchOptions.value 我必须返回一个结果对象的 “ 最终 ”,其含有所有“Alltrips”键值包含searchOptions.value对
for example for the given array "searchOptions" above, my resultant "final" object should be: 例如,对于上面给定的数组“ searchOptions”,我得到的“最终”对象应为:
final= {
"20180406": [{
"transporter": {
"company": "tes Transport",
"id": 15184195663555
},
"vehicleRegistrationNumber": "AS01FF1444",
"destinations": [{
"Id": "pcqYK",
"v": "Coorg"
}, {
"Id": "fy3yF",
"v": "Tiruppur, TN"
}],
"Owner": "pyu"
}
]
}
My solution I have tried is : 我尝试过的解决方案是:
var final={};
var temp=[];
for(var dates in this.alltrips) {
if(dates>=mindate && dates <=maxdate) {
for(var i=0;i<this.alltrips[dates].length; i++) {
//for(let options in this.alltrips[dates][i]) {
for(var j=0;j<searchOptions.length;j++) {
if(searchOptions[j].type== "TransporterType") {
var flag=true;
if(searchOptions[j].value == this.alltrips[dates][i].transporter.company)
flag=true
else {
flag=false
}
}
else if(searchOptions[j].type== "VehicleType"){
if(searchOptions[j].value == this.alltrips[dates][i].vehicleRegistrationNumber)
flag=true
else
flag=false
}
if(searchOptions[j].type== "PlaceType") {
//if(this.alltrips[dates][i]== 'destinations') {
for(var k=0;k<this.alltrips[dates][i].destinations.length;k++){
if(searchOptions[j].value== this.alltrips[dates][i].destinations[k].v)
flag=true
else
flag=false
}
// }
}
if(flag==true) {
temp.push(this.alltrips[dates][i])
}
else
break;
final[dates]= temp;
temp=[]
}
//}
}
}
}
console.log(temp)
console.log(final)
The problem with the above code is it is returning only single values for each keys like: 上面的代码的问题在于,每个键仅返回单个值,例如:
{"20180406" : Array(1)}
If I have more than one match for each keys than only one is returning. 如果每个键有多个匹配项,则只有一个匹配项返回。 For example, I might have some matches where the results might be like :
例如,我可能有一些匹配,结果可能像这样:
{"20180406" : Array(2), "20180412" : Array(2)}
but instead, for any matches, its returning only one array match . 但是,对于任何匹配项,它仅返回一个array match。 How do I solve it?
我该如何解决?
EDIT: I found the answer and updating the main function. 编辑:我找到了答案并更新了主要功能。 Using "continue" did the trick
使用“继续”就可以了
var final = {};
var temp = [];
var flag=false;
for (var dates in this.alltrips) {
temp=[]
if(dates >= mindate && dates <= maxdate) {
loop1 : for (var i = 0; i < this.alltrips[dates].length; i++) {
for(var j = 0; j < samplearray.length; j++) {
if (samplearray[j].type == "VehicleType") {
if (samplearray[j].value == this.alltrips[dates][i].vehicleRegistrationNumber) {
flag=true;
}
else {
flag=false;
continue loop1;
}
} else if (samplearray[j].type == "TransporterType") {
if (samplearray[j].value == this.alltrips[dates][i].transporter.company) {
flag=true;
}
else{
flag= false;
continue loop1;
}
} else if(samplearray[j].type == "PlaceType") {
for(var k=0;k< this.alltrips[dates][i].destinations.length;k++) {
if (samplearray[j].value == this.alltrips[dates][i].destinations[k].v) {
flag=true;
}
else {
flag= false;
continue loop1;
}
}
} else if (samplearray[j].type == "OwnerType") {
if (samplearray[j].id == this.alltrips[dates][i].operatorId) {
flag=true;
}
else{
flag= false;
continue loop1;
}
}
}
if(flag==true) {
temp.push(this.alltrips[dates][i])
final[dates]= temp;
}
}
}
}
console.log(final)
This works fine: 这工作正常:
<html>
<body>
<div id="result"></div>
</body>
<script>
Alltrips = {
"20180301": [{
"transporter": {
"company": "Pradeep Transport",
"id": 1518419163555
},
"vehicleRegistrationNumber": "AS01FF1234",
"destinations": [{
"Id": "pcwYK",
"v": "Bhubaneswar, OD"
}, {
"Id": "fy3yF",
"v": "Tiruppur, TN"
}],
"Owner": "Gogs"
},
{
"transporter": {
"company": "tes Transport",
"id": 1518419163515
},
"vehicleRegistrationNumber": "AS01FF1454",
"destinations": [{
"Id": "coWoz",
"v": "Vizag, AP"
}, {
"Id": "EZuh7",
v: "Ambala Sadar, HR"
}],
"Owner": "Acme"
}
],
"20180406": [{
"transporter": {
"company": "tes Transport",
"id": 15184195663555
},
"vehicleRegistrationNumber": "AS01FF1444",
"destinations": [{
"Id": "pcqYK",
"v": "Coorg"
}, {
"Id": "fy3yF",
"v": "Tiruppur, TN"
}],
"Owner": "pyu"
}
],
"20180412": [{
"transporter": {
"company": "tes Transport",
"id": 15184195663555
},
"vehicleRegistrationNumber": "AS01FF1444",
"destinations": [{
"Id": "pcqYK",
"v": "Coorg"
}, {
"Id": "fy3yF",
"v": "Tiruppur, TN"
}],
"Owner": "ps"
}]
};
searchOptions = [
{
label: "tes cargo",
value: "tes cargo",
type: "TransporterType"
},
{
label: "AS01FF1444",
value: "AS01FF1444",
type: "VehicleType"
},
{
label: "Coorg State, Kodagu Dist., Karnataka",
value: "Coorg State, KA",
type: "PlaceType"
}
];
window.onload = function() {
var resultDiv = document.getElementById('result');
var final = {};
var temp = [];
for (var dates in Alltrips) {
for (var i = 0; i < Alltrips[dates].length; i++) {
for (var j = 0; j < searchOptions.length; j++) {
console.log(searchOptions[j]);
console.log(Alltrips[dates][i]);
if (searchOptions[j].type == "TransporterType") {
var flag = true;
if (searchOptions[j].value == Alltrips[dates][i].transporter.company) {
console.log("found")
flag = true
} else
flag = false
} else if (searchOptions[j].type == "VehicleType") {
if (searchOptions[j].value == Alltrips[dates][i].vehicleRegistrationNumber) {
console.log("found")
flag = true
} else
flag = false
} else if (searchOptions[j].type == "PlaceType") {
for (var k = 0; k < Alltrips[dates][i].destinations.length; k++) {
if (searchOptions[j].value == Alltrips[dates][i].destinations[k].v) {
console.log("found")
flag = true
} else
flag = false
}
}
if (flag == true) {
temp.push(Alltrips[dates][i])
final[dates] = temp;
}
console.log(dates);
temp = []
}
}
}
console.log(temp)
console.log(final)
document.getElementById("result").innerHTML = JSON.stringify(final);
};
</script>
</html>
of course it doesn't match anything as is, but if you change the search filter so it matches something in Alltrips it works. 当然,它不匹配任何内容,但是如果您更改搜索过滤器,使其与Alltrips中的某些内容匹配,则它会起作用。 https://jsfiddle.net/zs8xmaLx/35/
https://jsfiddle.net/zs8xmaLx/35/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.