简体   繁体   English

我在将对象添加到数组时遇到问题

[英]I have problem with adding my object to array

So after creating an object,I want to add it to an array but seem it doesn't working Only the last one was added and it looped.所以在创建一个对象之后,我想将它添加到一个数组中,但似乎它不起作用只添加了最后一个并且它循环了。

I expect the output of mang[0] would be ('Samsung S8','s0','$200') but it doesn't.我希望mang[0]的输出是('Samsung S8','s0','$200')但它不是。

It only show the last one ('Samsung A60','s2','$400') .它只显示最后一个('Samsung A60','s2','$400') It's the same to mang[1] . mang[1]也是一样。

var mang=[];
 var Phone = {
            Name:'',
            Img:'',
            Price:'',
            them :function(name,img,price){
                this.Name=name;
                this.Img=img;
                this.Price=price;
                mang.push(Phone);
            }
        };
Phone.them('Samsung S8','s0','$200');
Phone.them('Samsung S10','s1','$300');
Phone.them('Samsung A60','s2','$400');

Hi please try this solution嗨,请尝试此解决方案

var PhoneList = [];
function Phone(Name,Img,Price){
    this.Name = Name; 
    this.Img = Img;
    this.Price = Price ; 
}


Phone.prototype.addToArray = function(){
    PhoneList.push(this); 
}

let phone1 = new Phone('Samsung S8','s0','$200');
phone1.addToArray();
let phone2 = new Phone('Samsung S10','s1','$300');
phone2.addToArray();
let phone3 = new Phone('Samsung A60','s2','$400');
phone3.addToArray();

console.log(PhoneList);

I hope it helps.我希望它有帮助。

What you could consider is escapsulating that functionality in a class , where you can have a list of phones that you add to with one method ( add ), and another ( logPhones ) to log the list of phones that have been added.您可以考虑在一个class封装该功能,您可以在其中使用一种方法 ( add ) 添加电话列表,并使用另一种方法 ( logPhones ) 记录已添加的电话列表。

 class Phones { constructor() { this.phoneList = []; } add = (name, img, price) => { const phone = { name, img, price }; this.phoneList.push(phone); } logPhones = () => { console.log(this.phoneList); } } const phones = new Phones(); phones.add('Samsung S8','s0','$200'); phones.add('Samsung S10','s1','$300'); phones.add('Samsung A60','s2','$400'); phones.logPhones();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript 分配有问题:数组和 object 迭代但我不知道解决方案是什么,我已经尝试了一切 - Having a problem with a Javascript assignment: Array and object iteration but I have no idea what the solution is, I have tried everything 单击“未定义”后,向页面添加元素时出现问题,我该怎么办? - I have a problem with adding elements to my page after click - “Undefined”, what I need to do? 我试图将一个对象添加到我存储在我的状态对象中的嵌套数组中,但似乎有问题 - I am trying to add an object to a nested array I have stored in my state object, but seem to have an issue with it XMLHttpRequest - 当REST API返回JSON对象而不是对象数组时,我遇到了问题 - XMLHttpRequest - I have problem when REST API returns JSON object and not array of object 我对数组排序有问题,我不知道为什么 - I have problem with sorting my array and I don`t know why 在JavaScript中向数组内的特定对象添加对象属性时出现问题 - Problem in adding object properties to specific object inside array in JavaScript 我试图写一个从数组创建一个简单的对象。 在我的数组中,我存储了变量: - I am trying to write a create a simple object from an array. In my array, I have variable stored: 我在使用 ckeditor 上传图片时遇到问题 - I have a problem with uploading my images with ckeditor 我的 JavaScript 秒表有问题 - I have a problem with my stop watch in JavaScript 我的 React 版本有问题 - I have a problem with my version of React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM