我有
var val = "2520,3569,99991,14203,11918";
也 我的测试包含以下格式的所有值(附带截图),其中包含所有测试值。 在完整列表中,我只需要几个值,我可以通过下面的字符串过滤它。这意味着如果我有上述任何一个ID,我必须考虑它。
例如,如果我的测试包含以下任何值:
var val= "2520,3569,99991,14203,11918";
,然后我需要它
2520.1|3569.0|99991.1|14203.0|11918.0
,
忽略所有其他值。 (其中2520是id,1/0是值)
答案 0 :(得分:0)
希望这是你想要的,试试这个答案,
marketingABTests = "2520,3569,99991,14203,11918".split(",");
expABTests = [
{
"experimentId" : 2520,
"bucketValue" : 1,
},
{
"experimentId" : 3569,
"bucketValue" : 0,
},
{
"experimentId" : 11918,
"bucketValue" : 0,
},
{
"experimentId" : 99991,
"bucketValue" : 1,
}
]
newMarketABTest = ""
for (var i in marketingABTests) {
curr_i = eval(marketingABTests[i]);
find_val = _.find(expABTests, function(num) { return num.experimentId == curr_i });
if(find_val) {
newMarketABTest+= find_val.experimentId+"."+find_val.bucketValue+"|"
}
}
newMarketABTest = newMarketABTest.substring(0, newMarketABTest.length - 1);
console.log(newMarketABTest);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
答案 1 :(得分:0)
$(document).ready(function() {
var myObject = [{
bucketValue: 1,
exprimentId: 56988
}, {
bucketValue: 1,
exprimentId: 2520
}, {
bucketValue: 0,
exprimentId: 3569
}, {
bucketValue: 1,
exprimentId: 99991
}, {
bucketValue: 0,
exprimentId: 14203
}, {
bucketValue: 0,
exprimentId: 11918
}, {
bucketValue: 1,
exprimentId: 1111
}, {
bucketValue: 0,
exprimentId: 22222
}];
var marketingABTests = "2520,3569,99991,14203,11918";
var myValues = marketingABTests.split(",");
var myFinalValue = "";
for (i = 0; i < myValues.length; i++) {
for (j = 0; j < myObject.length; j++) {
//alert(myValues[i] + " " + myObject[j].exprimentId);
if (myFinalValue == "" && myValues[i].toString().trim() == myObject[j].exprimentId.toString().trim()) {
myFinalValue = myObject[j].exprimentId.toString().trim() + "." + myObject[j].bucketValue.toString().trim();
} else if (myFinalValue != "" && myValues[i].toString().trim() == myObject[j].exprimentId.toString().trim()) {
myFinalValue = myFinalValue + "|" + myObject[j].exprimentId.toString().trim() + "." + myObject[j].bucketValue.toString().trim();
}
}
}
document.getElementById("demo").innerHTML = myFinalValue;
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<p id="demo">
</p>
&#13;