Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/425.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 试图确定一个对象是否在数组中,如果是,则将对象推到数组中,如果不是,则执行其他操作_Javascript_Arrays_Vue.js - Fatal编程技术网

Javascript 试图确定一个对象是否在数组中,如果是,则将对象推到数组中,如果不是,则执行其他操作

Javascript 试图确定一个对象是否在数组中,如果是,则将对象推到数组中,如果不是,则执行其他操作,javascript,arrays,vue.js,Javascript,Arrays,Vue.js,让我来编辑一下,我一开始觉得有点困惑 好的,这是一个学校项目。我不是要求任何人做这件事,我只是被困在一部分。我只是需要如何摆脱困境的指导。我正在使用Vue.js制作一个非常基本的购物车。当用户单击按钮将项目添加到购物车时,对象进入一个名为GamesBuw的数组。我需要它来确定数组中是否有特定的对象,如果没有,我需要将对象推送到数组中。在我的代码中,我已经创建了3个对象,每个对象都有一个唯一的名称。我需要确定它们中是否有一个已经在数组中,以及是哪一个。我在代码中有两次尝试,一次被注释掉。我在堆栈溢

让我来编辑一下,我一开始觉得有点困惑

好的,这是一个学校项目。我不是要求任何人做这件事,我只是被困在一部分。我只是需要如何摆脱困境的指导。我正在使用Vue.js制作一个非常基本的购物车。当用户单击按钮将项目添加到购物车时,对象进入一个名为GamesBuw的数组。我需要它来确定数组中是否有特定的对象,如果没有,我需要将对象推送到数组中。在我的代码中,我已经创建了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;
        }
    }
});

可以使用.constructor属性确定对象类型Object vs.Array。因此,给定一个名为Arr的数组,执行Arr.constructor,它将返回“array”,如果它是一个对象,它将返回“object”

您可以在此处找到示例:

让某物=[1,2,3,4]; 让某物={ 答:1,, b:2 }; console.logsomething.constructor; //在if语句中 如果something.constructor==数组{ log'something is a Array'; }
console.logsomethingelse.constructor 可以使用array.isArray确定对象是否为数组:


也就是说,基于变量类型的切换逻辑通常是设计不良的标志;您正在编写代码,因此您应该已经知道您的变量是什么类型。

好吧,我看错了问题,做错了,我已经完成了,并且按照我需要的方式工作。谢谢大家花时间回答我的问题

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