我有以下脚本。
我试图在调用saySomething()函数后设置showMsg变量。它需要设置为true或false,具体取决于我对saySomething函数的处理方式。
有了这个,我得到了不确定。在调用saySomething函数后如何设置showMsg?
$(document).ready(function(){
var showMsg;
(function() {
saySomething("something something darkside, something something complete!",
function() { showMsg = true; console.log('aww yis') },
function() { showMsg = false; console.log('ooo noo') } );
})();
console.log(' showMsg = ' + showMsg);
function saySomething(strText, functionOne,functionTwo){
console.log("From saySomething : " + strText);
}
});
答案 0 :(得分:0)
你必须实际执行其中一个功能才能产生效果。我不知道你的逻辑是什么,但是类似于以下内容:
$(document).ready(function(){
var showMsg;
(function() {
saySomething("something something darkside, something something complete!",
function() { showMsg = true; console.log('aww yis') },
function() { showMsg = false; console.log('ooo noo') } );
})();
console.log(' showMsg = ' + showMsg);
function saySomething(strText, functionOne,functionTwo){
alert(strText);
if (strText === "something something darkside, something something complete!") {
functionOne();
} else {
functionTwo();
}
}
});
答案 1 :(得分:0)
我想你想要这样的东西
const listings = [{ // sample listings with multiple listing_id and duplicates
"listing_id": 1,
"furniture_id": 2,
"price": 129.99,
"duration": null,
"photo_id": 1,
"url": "http://d3otkl9byfilk1.cloudfront.net/images/COHG3---A_Graceland-Silver-With-Malva-Blue-Grey-Contrast"
}, {
"listing_id": 1,
"furniture_id": 2,
"price": 129.99,
"duration": null,
"photo_id": 2,
"url": "https://www.responsive-checkout.com/demo/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/c/o/couch.jpg"
}, {
"listing_id": 1,
"furniture_id": 2,
"price": 129.99,
"duration": null,
"photo_id": 2,
"url": "https://example.com"
}, {
"listing_id": 2,
"furniture_id": 2,
"price": 129.99,
"duration": null,
"photo_id": 2,
"url": "https://www.responsive-checkout.com/demo/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/c/o/couch.jpg"
}, {
"listing_id": 2,
"furniture_id": 2,
"price": 129.99,
"duration": null,
"photo_id": 2,
"url": "https://example2.com"
}, {
"listing_id": 3,
"furniture_id": 2,
"price": 129.99,
"duration": null,
"photo_id": 2,
"url": "https://example3.com"
}];
const duplicates = [];
const filteredListings = listings.filter((listing, index, self) => { // filter out all duplicates
const found = self.findIndex(i => i.listing_id === listing.listing_id);
if (found !== index) {
duplicates.push(listing); // add duplicates to duplicate array to be added back in later.
return false;
} else return true;
});
duplicates.forEach((duplicate) => { // loop through duplicates to add url back into main array
const listIndex = filteredListings.findIndex(listing => listing.listing_id === duplicate.listing_id);
if (Array.isArray(filteredListings[listIndex].url)) {
filteredListings[listIndex].url.push(duplicate.url);
} else filteredListings[listIndex].url = [filteredListings[listIndex].url, duplicate.url];
});
document.body.innerHTML = '<pre>' + JSON.stringify(filteredListings, 0, 4) + '</pre>';
这里有一个关于jsfiddle的例子。 [https://jsfiddle.net/dau7awtk/]
希望这可以帮助你。