我想让我编辑一下,一开始我有点困惑。
好,这是一个学校项目。我不是要任何人去做,我只停留在一部分上。我只需要有关如何解粘的指南。我正在使用Vue.js制作非常基本的购物车。当用户单击按钮以将商品添加到购物车时,对象将进入名为gamesBought的数组。我需要它能够确定数组中是否存在特定对象,如果不是,则需要将该对象推送到数组中。在我的代码中,我已经制作了3个对象,每个对象都有一个唯一的名称。我需要确定其中之一是否已经在数组中以及哪一个。我有2次尝试在代码中被注释掉了。我在堆栈溢出时查找了它们,但无法正常工作。
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;
}
}
});
答案 0 :(得分:1)
您可以使用Array.isArray
确定对象是否为数组:
Array.isArray([]) // true
Array.isArray({length: 0}) // false
话虽如此,基于变量类型的切换逻辑通常表示设计不良;您正在编写代码,因此您应该已经知道变量是什么类型。
答案 1 :(得分:0)
您可以使用.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);
答案 2 :(得分:0)
好吧,我当时看错了问题,然后又去做错了,我已经完成了工作,并按照需要的方式工作。感谢您抽出宝贵时间回答我的问题。
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;
}