简体   繁体   English

我只想从数组中打印第一个值(名称)

[英]i just want to print the first value(name) from the array

In the below code I am trying to print out just the first value (name) of the array, but it doesn't work as I expect: 在下面的代码中,我试图仅打印出数组的第一个值(名称),但是它无法正常工作:

function Person (name, age) {
  this.name = name;
  this.age = age;
}// Our Person constructor


// Now we can make an array of people
var family = new Array();
family[0] = new Person("alice", 40);
family[1] = new Person("bob", 42);
family[2] = new Person("michelle", 8);
family[3] = new Person("timmy", 6);
// loop through our new array
for(i = 0; i <= family.Length; i++) {
  console.log( family[i].this.name); 
}

You are using the "this" keyword incorrectly. 您错误地使用了“ this”关键字。 When you access family[i] you are already accessing an instance of that prototype in JavaScript. 当您访问family [i]时,您已经在JavaScript中访问该原型的实例。 Just drop the "this." 只需删除“ this”。

To get the first item from the array you could do the below without the loop: 要从数组中获取第一项,可以执行以下操作而无需循环:

console.log(family[0].name);

Without looping, as the loop is unnecessary if you know which item you want to print. 没有循环,因为如果知道要打印的项目,则不需要循环。

Or, if the loop is necessary you could add some logic such as 或者,如果需要循环,则可以添加一些逻辑,例如

if(i === 0) {
  console.log(family[0].name);
}

You do not need to use this when accessing the name property of the object in the array. 访问数组中对象的name属性时,无需使用this属性。

function Person (name, age) {
  this.name = name;
  this.age = age;
}// Our Person constructor


// Now we can make an array of people
var family = new Array();
family[0] = new Person("alice", 40);
family[1] = new Person("bob", 42);
family[2] = new Person("michelle", 8);
family[3] = new Person("timmy", 6);

// loop through our new array
for(i = 0; i < family.length; i++) {
  console.log( family[i].name); 
}

暂无
暂无

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

相关问题 我想多次打印数组值 - I want print array value multiple times 我想打印从 0 到 1000 的“i”值 - i want to print the value of “i” from 0 to 1000 如何处理数组,如果我只想单击时从中返回一个特定值 - how to deal with an array, if I just want return one specific value from it when clicking 我在 Js 中有一个包含 1 到 30 的数组集合,单击按钮时,我想从中获取第一个混洗值 - I have a collection of array in Js contains 1 to 30, on button click, i want to get the first shuffled value from it 我使用 for 为数组赋值,但是当我打印数组时,它只是在整个数组中的最后一个值 - im using for to assign a value to an array but when I print the array it just shoes the last value in the full array 我只想从关联数组中打印一次重复值 - I want to print only the repeated values once from an associative array 我正在从刀片模板传递数组“ slot”,并希望在js文件中获取其值,例如name = slot [&#39;name&#39;];。 - i am passing an array “slot” from blade template and want to get its value in js file like name=slot['name']; 从 REST API 返回数组中的前 2 个 json 名称/值对 - Return first 2 json name/value pairs in an array from a REST API 我有两个数组,我需要打印第一个数组中的第一个元素和第二个数组中的第一个元素,依此类推 - I have two arrays, i need to print first element from first array and first element from second array and so on 我想打印数组的最后一个 &#39;n&#39; 元素 - I want to print the last 'n' elements of an array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM