简体   繁体   English

如何访问对象属性数组?

[英]How to Access an array of objects properties?

i'm working in a simple program that uses an HTML Form to fill an Array with some information, so far, i can get the input data, store it into my list, and visualize it like this : 我正在一个简单的程序中工作,该程序使用HTML表单将一些信息填充到数组中,到目前为止,我可以获取输入数据,将其存储到我的列表中,并像这样可视化它: 在此处输入图片说明

It basically, converts the Name to a Link, when you click it, it will create a <div> in which i show all the information of the contact. 基本上,它将名称转换为链接,当您单击它时,它将创建一个<div> ,其中我显示联系人的所有信息。

And i've done this like this : 我这样做是这样的:

The error i'm getting is in the last 6 lines of Code. 我得到的错误是在代码的最后6行中。

(I'm trying to avoid all non-troubling code) (我试图避免所有非麻烦的代码)

var list = [];
var ulList = document.createElement("UL");

function AddToList(){
    //Just pushes the info into the list.
}

function Visualize(){
    ClearScreen();
    for(var i = 0 ; i < list.length ; i++){
        //Tried to keep it clean, this just works with each item in the list.
        AddToList(i);    
    }      
}

//This Works correctly, it shows every Name i've previously pushed into the list like a Link.
function AddToList(index){
    var element = document.createElement("LI");
    var name = document.createTextNode(list[index].name);
    element.appendChild(name);

    var link = document.createElement("A");
    link.setAttribute("HREF", "#");
    link.appendChild(element);
    lik.setAttribute("ID", index);            
    link.addEventListener("click", ShowInfo(this.id)); //Index would do the same

    ulList.appendChild(link);           
    document.getElementsByTagName("body")[0].appendChild(ulList);    
}

//Trouble comes here
function ShowInfo(index){
    CleanDIV();

    //Previously created <div> with box as id    
    var box = document.getElementById("box");
    var details = document.createElement("UL");
    var lName = document.createElement("LI");
    var lNumber = document.createElement("LI");
    var lMail = document.createElement("LI");


    //
    //The error is here : Cannot Read Property 'name' of undefined
    //And i dont know why, since i've done something similar in a previous line...
    //    
    lName.appendChild(document.createTextNode("Name :" + list[index].name));
    lNumber.appendChild(document.createTextNode("Number : " + list[index].number));
    lMail.appendChild(document.createTextNode("Mail : " + list[index].mail));

    details.appendChild(lName);
    details.appendChild(lNumber);
    detaisl.appendChild(lMail);
}

I dont even know what kind of mistake i'm making, and was not sure of how to ask this question. 我什至不知道我犯了什么样的错误,并且不确定如何问这个问题。

I apologyze for any grammar mistake, my bad variable naming abilities and any lack of quality in my question. 我为任何语法错误,错误的变量命名能力以及问题中的任何质量欠缺而道歉。

Thank you kindly. 非常感谢你。

在您的AddToList函数中,索引是“未定义的”

You get the error because you have not added anything to your list array. 因为没有将任何内容添加到list数组,所以收到错误消息。 Add the person details to the list array when the Add to List button is clicked. 单击“ 添加到列表”按钮时,将人员详细信息添加到 list数组。 And retrieve this when the link is clicked. 并在单击链接时检索它。

var name = document.getElementById("txtName").value;
var number = document.getElementById("txtNnumber").value;
...
var person = {'name': name, 'number': number};
list.push(person);

Looks like you have a couple of typos in your code. 看起来您的代码中有几次错别字。

lik.setAttribute("ID", indice);            
link.addEventListener("click", ShowInfo(this.id)); //Index would do the same

should be 应该

link.setAttribute("ID", index);            
link.addEventListener("click", ShowInfo(index)); //Index would do the same

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

相关问题 如何在 react/javascript For-Loop & ForEach 中访问对象数组中的属性? - How to access properties in array of objects in react/javascript For-Loop & ForEach? 如何遍历包含对象的数组并访问它们的属性 - How to loop through an array containing objects and access their properties 无法访问数组内 json 对象的属性 - cannot access properties of json objects inside array 将对象的属性访问到嵌套数组中 - Javascript - Access properties of objects into nested Array - Javascript 如何遍历对象数组并访问每个数组内的属性以显示在 html 元素中? - how to loop through an array of objects and access the properties inside each array to display in an html element? 如何从对象数组的属性返回数组 - How to return an array from properties of an array of objects 如何访问解析对象属性? - How do I access a Parse objects properties? 如何访问对象数组中的不同属性-每个对象都有不同的键 - How to access different properties in array of objects - each object has different keys 如何将一个对象数组的属性转换为另一个对象数组 - How to get properties of one array of objects into another array of objects 如何获取对象数组中对象的所有属性值的数组? - How to get an array of the values of all properties of objects inside an array of objects?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM