试图确定一个对象是否在一个数组中,如果是这样推送对象,如果不是我做其他事情

问题描述 投票:0回答:3

让我编辑一下这让我觉得有点困惑。

好的,这是一个学校项目。我不是要求任何人这样做我被困在一个部分。我只需要指导如何解开。我正在使用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;
        }
    }
});
javascript arrays vue.js
3个回答
1
投票

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

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

话虽如此,基于可变类型的切换逻辑通常表示设计不良;你正在编写代码,所以你应该已经知道你的变量是什么类型。


0
投票

您可以使用.constructor属性确定对象类型(对象与数组)。所以给定一个名为Arr的数组,执行Arr.constructor并返回'Array',如果它是一个对象,它将返回'Object`'

你可以在这里找到例子:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor#Examples

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);

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;
            }
© www.soinside.com 2019 - 2024. All rights reserved.