我无法根据对象在单独数组中的值来生成脚本以匹配对象数组中对象的值,并从该对象中检索单独的值。
我已经在jQuery中使用了标准for循环和当前迭代。
我还尝试设置if
语句以查找两个值==
,但它总是产生非匹配(或-1)。
有人能引导我朝着正确的方向前进吗?
transfers = [
{Package: "1", Origin_Facility = "a"},
{Package: "2", Origin_Facility = "b"}
];
storeData = [
{fromPackage: "1,6,26"}
]
var storeDataEach = function( sx, sxv ) {
var transfersEach = function( sy, syv ) {
if(storeData[sx].fromPackage.indexOf(transfers[sy].Package) > -1){
var facilityStore = transfers[sx].Origin_Facility;
storeData[sx].origin = facilityStore + " + " + transfers[sy].Package + ' + ' + storeData[sx].fromPackage;
return false;
} else {storeData[sx].origin = 'error' + transfers[sy].Package + " + " + storeData[sx].fromPackage;return false;}
};
jQuery.each(transfers, transfersEach);
}
jQuery.each(storeData, storeDataEach);
答案 0 :(得分:1)
主要问题是你从false
循环返回$.each
将停止迭代
原始修复是从else块中删除return
var storeDataEach = function(sx, sxv) {
var transfersEach = function(sy, syv) {
if (storeData[sx].fromPackage.indexOf(transfers[sy].Package) > -1) {
var facilityStore = transfers[sx].Origin_Facility;
storeData[sx].origin = facilityStore + " + " + transfers[sy].Package + ' + ' + storeData[sx].fromPackage;
return false;
} else {
storeData[sx].origin = 'error' + transfers[sy].Package + " + " + storeData[sx].fromPackage;
}
};
jQuery.each(transfers, transfersEach);
}
但是这仍然存在数据结构问题,在您的示例中,您在fromPackage
中有26个,现在如果您的package
值为2,那么也会返回正数结果