简体   繁体   English

使用JavaScript从数组访问元素名称(无值)

[英]Access element name (no value) from array with javascript

Sorry if my question is bad. 对不起,如果我的问题不好。 I'm stuck in the following simple javascript code: 我陷入了以下简单的javascript代码中:

var person={fname:"John",lname:"Doe",age:25};

for (x in person)
{
document.write(person[x] + " ");
}

The above code result is : John Doe 25 上面的代码结果是:John Doe 25

How do you show only value from lname element ? 如何仅显示lname元素的值? (with for (... in ...) statement) (带有for (... in ...)语句)

I don't want to use person={"John","Doe",25} instead of one of above. 我不想使用person={"John","Doe",25}而不是上面的一个。 I want to know how to access the element from array to get value. 我想知道如何从数组访问元素以获取价值。

Thank you 谢谢

Edit: Thanks all for responses. 编辑:谢谢大家的答复。 This question was created because I have many items in array like : 创建此问题是因为我在数组中有很多项目,例如:

        [{Name="A 1", Email="a1@manufacturer.com", Group="Group1"},
        {Name="A 2", Email="a2@manufacturer.com", Group="Group2"},
        {Name="A 3", Email="a3@manufacturer.com", Group="Group3"},
        {Name="B 1", Email="b1@manufacturer.com", Group="Group1"},
        {Name="V 1", Email="b1@manufacturer.com", Group="Group2"},
        {Name="X 1", Email="b1@manufacturer.com", Group="Group4"},
        {Name="Y 1", Email="b1@manufacturer.com", Group="Group3"},
        {Name="Z 1", Email="b1@manufacturer.com", Group="Group3"},
        {Name="W 1", Email="b1@manufacturer.com", Group="Group6"}]

I want to iterate this array and compare the element with my owned value. 我想迭代此数组并将元素与我拥有的值进行比较。 if object1.Group == 'Group3' { code }

What you have here is not an array, it is a JavaScript object. 您在这里拥有的不是数组,而是一个JavaScript对象。 (In simple terms,) Arrays have numeric indexes, while objects have properties with property-name and value pairs. (简单来说)数组具有数字索引,而对象具有带有属性名称和值对的属性。

To access a specific object property like "lname" simply say: 要访问特定的对象属性,例如“ lname”,只需说:

person.lname
// OR
person["lname"]

Or, if the name of the property you want to use is in a variable: 或者,如果您要使用的属性名称在变量中:

var whichProp = "lname";
person[whichProp]

With your data, to produce the output "John Doe" you'd say: 使用您的数据,要产生输出“ John Doe”,您会说:

document.write(person.fname + " " + person.lname);

The for (x in person) syntax that you were using loops through every property of the person object, setting x to each property name in turn. 您使用的for (x in person)语法在person对象的每个属性中循环,依次将x设置为每个属性名称。

UPDATE FOR YOUR UPDATED QUESTION: 更新您的更新问题:

Assuming you have an array of objects: 假设您有一个对象数组:

var people = [{Name:"A 1", Email:"a1@manufacturer.com", Group:"Group1"},
              {Name:"A 2", Email:"a2@manufacturer.com", Group:"Group2"},
              {Name:"A 3", Email:"a3@manufacturer.com", Group:"Group3"},
              // etc ];

And you want to find the element where Group is "Group3" then you loop through the array with a traditional for loop as follows: 您想找到Group为“ Group3”的元素,然后使用传统的for循环遍历数组,如下所示:

for (var i=0; i < people.length; i++) {
   if (people[i]["Group"] === "Group3") {
      // do something
   }
}

Within the if statement you can add a break statement if you want to stop processing after the first match. 如果要在第一个匹配项后停止处理,可以在if语句中添加break语句。 And of course you are free to use the people[i].Group syntax rather than people[i]["Group"] if you prefer it. 当然,如果愿意,您可以自由使用people[i].Group语法,而不是people[i]["Group"]

(Note also that your object literal syntax was incorrect in your question update: where you've used = you should've used : as shown in my answer above.) (还请注意,在问题更新中,对象字面量语法不正确:您使用的位置=您应该使用的:如上面我的答案所示。)

var arr = [];
var person={fname:"John",lname:"Doe",age:25};
arr.push(person);

for (var i=0; i < arr.length; i++)
{
   document.write(arr[i].lname+ " ");
}

Update 更新资料

JsFiddle example JsFiddle示例

您可以像这样访问person对象的两个属性fnamelname

document.write(person.fname + " " + person.lname);

That's not an Array, that's an Object. 那不是数组,那是一个对象。 If you know the property you want to access, you don't need to iterate the Object. 如果知道要访问的属性,则不需要迭代Object。 Just reference the property directly. 只需直接引用该属性。

person.lname;

Edit: To iterate your Array and take action when the group matches: 编辑:要迭代您的数组并在组匹配时采取措施:

for (var i = 0; i < objects.length; i++) {
    if (objects[i].Group == "Group3") {
        // do something 
    }
}

But, the code you posted is invalid. 但是,您发布的代码无效。 Property/value pairs are separated with a : , not = . 属性/值对以:分隔,而不是=

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM