让我编辑一下,我想一开始我把它弄得有点混乱。
好的,这是一个学校项目。我没有要求任何人来做这件事,我被困在了一个部分。我只需要如何解脱的指导。我正在使用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;
}
}
});发布于 2018-12-05 08:28:42
您可以使用Array.isArray确定对象是否为数组
Array.isArray([]) // true
Array.isArray({length: 0}) // false也就是说,基于变量类型的切换逻辑通常表示糟糕的设计;您正在编写代码,因此您应该已经知道您的变量是什么类型。
发布于 2018-12-05 08:24:50
可以使用.constructor属性确定对象类型(对象与数组)。因此,给定一个名为Arr的数组,执行Arr.constructor操作,它将返回' array ',如果它是一个对象,它将返回'Object`‘。
您可以在此处找到示例:
let something = [1, 2, 3, 4];
let somethingelse = {
a: 1,
b: 2
};
console.log(something.constructor);
//in an if statement
if (something.constructor == Array) {
console.log('something is an Array');
}
console.log(somethingelse.constructor);
发布于 2018-12-05 10:28:21
好吧,我看错了问题,做错了,我已经把它做好了,并按照我需要的方式工作。感谢那些花时间回答我的问题的人。
if(this.gamesBought.length > 0){
if(theItem.sold == true){
if(theItem.quantity >= 0){
theItem.ordered++;
theItem.total = theItem.price * theItem.ordered;
theItem.quantity--;
this.total += theItem.total;
}else{
this.disabled = 1;
theItem.quantity = 0;
}
}else{
theItem.sold = true;
theItem.ordered++;
theItem.total = theItem.price * theItem.ordered;
this.gamesBought.push(theItem);
theItem.quantity--;
if(theItem.quantity == 0){
this.disabled = 1;
theItem.quantity = 0;
}
this.total += theItem.total;
}
}else{
theItem.sold = true;
theItem.ordered++;
theItem.total = theItem.price * theItem.ordered;
this.gamesBought.push(theItem);
theItem.quantity--;
if(theItem.quantity == 0){
this.disabled = 1;
theItem.quantity = 0;
}
this.total += theItem.total;
}https://stackoverflow.com/questions/53623362
复制相似问题