gpt4 book ai didi

javascript - 试图确定一个对象是否在数组中,如果是,则将对象推送到它,如果不是,我会做其他事情

转载 作者:行者123 更新时间:2023-12-03 00:36:51 24 4
gpt4 key购买 nike

让我编辑一下,一开始我觉得这有点令人困惑。

好的,这是一个学校项目。我不要求任何人这样做,我只是被困在一个部分上。我只需要指导如何摆脱困境。我正在使用 Vue.js 制作一个非常基本的购物车。当用户单击按钮将商品添加到购物车时,该对象将进入名为 gamesBought 的数组。我需要它能够确定特定对象是否在数组中,如果没有,我需要将该对象推送到数组中。在我的代码中,我已经创建了 3 个对象,每个对象都有一个唯一的名称。我需要确定其中之一是否已经在数组中以及是哪一个。我在代码中有两次尝试,其中之一被注释掉了。我在堆栈溢出上查找了它们,但无法让它工作。

var app = new Vue({
el:"#app",

data: {
items:
[
{name:"COD: Black Ops 4", quantity: 4, price: 49.99, ordered: 0, total: 0 ,imgSrc:"cod.png"},
{name:"Fallout 76", quantity: 6, price: 59.99, ordered: 0, total: 0, imgSrc:"fallout.png"},
{name:"Red Dead Redemption 2", quantity: 5, price: 39.99, ordered: 0, total: 0, imgSrc:"reddead.png"}
],
gameName: "",
netTotal: 0,
gamesBought: [],
descriptions: ["Black Ops 4 takes the top-selling franchise in Call of Duty® to new heights. The title is tailored to the millions of Call of Duty: Black Ops fans worldwide who continue to engage and play together. Forget what you know, Call of Duty: Black Ops 4 is set to deliver a revolutionary experience.","Do you have nerves of steel? An ironclad will? Average hygiene and an affinity for pre-packaged goods? Then Vault-Tec WANTS you! If you think you have what it takes to be a Vault 76 Test Subject – and enjoy prolonged confinement without sunlight – then you’re in luck! Join the proud ranks of Vault 76 today.","America, 1899. The end of the Wild West era has begun. After a robbery goes badly wrong in the western town of Blackwater, Arthur Morgan and the Van der Linde gang are forced to flee. With federal agents and the best bounty hunters in the nation massing on their heels, the gang must rob, steal and fight their way across the rugged heartland of America."]
},

methods: {
orderItem(theItem){

this.gameName = theItem.name;

for(game in gamesBought) {
var g = gamesBought.indexOf(game);
if(typeof gamesBought !== 'undefined' && gamesBought.length > 0) {
if(game.name == gamesBought[g].name){
theItem.ordered++;
theItem.total = theItem.price * theItem.ordered;
theItem.quantity--;
this.total += theItem.total;
}else
{
theItem.ordered++;
theItem.total = theItem.price * theItem.ordered;
this.gamesBought.push(theItem);
theItem.quantity--;
this.total += theItem.total;
}
}
if (!gamesBought.some(item => item === theItem)) {
gamesBought.push(theItem);
theItem.ordered++
theItem.total = theItem.price*theItem.ordered;
theItem.quantity--;
total += theItem.total;
}else{
ttheItem.ordered++;
theItem.total = theItem.price * theItem.ordered;
theItem.quantity--;
this.total += theItem.total;
}

// if(game.name == gamesBought[g].name){
// theItem.ordered++;
// theItem.total = theItem.price * theItem.ordered;
// theItem.quantity--;
// this.total += theItem.total;
// }else
// {
// theItem.ordered++;
// theItem.total = theItem.price * theItem.ordered;
// this.gamesBought.push(theItem);
// theItem.quantity--;
// this.total += theItem.total;
// }
}

},
removeItem(anItem){
theItem.ordered--;
var index = this.gamesBought.indexOf(anItem);
this.gamesBought.splice(index, 1);
theItem.quantity++;
this.total -= theItem.total;
}
}
});

最佳答案

您可以使用Array.isArray确定对象是否为数组:

Array.isArray([]) // true
Array.isArray({length: 0}) // false

话虽这么说,基于变量类型的切换逻辑通常表明设计不好;您正在编写代码,因此您应该已经知道变量的类型。

关于javascript - 试图确定一个对象是否在数组中,如果是,则将对象推送到它,如果不是,我会做其他事情,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53623362/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com