【问题标题】:How to return all the elements of an Array with space between them?如何返回数组的所有元素,它们之间有空格?
【发布时间】:2020-01-27 10:04:19
【问题描述】:

我正在创建一个带有一些属性和方法的构造函数。属性是数组。 在这些方法中,我想返回一个带有数组元素的短语。 默认情况下,数组的元素之间没有空格。然后我在方法内创建了一个 for 循环,以返回每个元素,其自身和逗号之间有空格。 事实证明,当我转到控制台并使用该方法调用稍后创建的实例时,它只显示数组的第一个元素。 有人可以向我解释我做错了什么以及我必须做什么吗?谢谢 这是我的代码:

function People(first, last, age, gender, interest, caracteristic ){
            this.name={
                first:first,
                last:last
            };

            this.age = age;
            this.gender = gender;
            this.interest = interest;
            this.caracteristic = caracteristic;

            this.bio = function() {
                return `${this.name.first} ${this.name.last} is ${this.age}.Is ${this.gender} and likes ${this.interest}`
            }

            this.greeting = function(){
                return ` Hi! I'm ${this.name.first}.`
            }

            this.personality = function(){
                *// here I try to find a way to show all the caracteristic with some space between*
                for(var i = 0 ; i <= caracteristic.length ; i++){
                    let caracteristica = caracteristic[i] + ' ,'
                    return caracteristica
                }               
                 return ` ${this.name.first} is ${caracteristica}`
            }

        }

        let people1 = new People('Sandra',
                                 'Stward', 
                                 47, 
                                 'female',
                                ['arts', 'cinema', 'poetry', 'photography', 'arts and crafts', 'painting', 'drawing', 'creative art'],
                                 ['kind', 'clever', 'sweet', 'empathic', 'emotive', 'hight sensitive person']
                                 )

    </script>

【问题讨论】:

  • 不要在 for 循环中返回。它总是返回第一个项目。考虑改用join
  • 真的吗?你能解释一下为什么吗?因为如果我写 index [i] ,不会全部显示出来吗?谢谢你的建议!
  • 为什么要运行它们?它将进入for循环查看返回然后在下一个循环运行之前离开函数。
  • for 循环 i &lt;= caracteristic.length 应该是 i &lt; caracteristic.lengthreturn caracteristica 应该在 for 循环之外或者只是删除它。

标签: javascript arrays loops object


【解决方案1】:

for 循环中的返回将很快结束循环。

但是要创建一个以空格分隔的字符串,最好使用连接。

    this.personality = function() {
          return this.name.first + ' is ' + this.caracteristic.join(' ');
    }

也就是说,您也可以使用Class 来代替函数。

示例 sn-p:

class People {
     constructor (first, last, age, gender, interest, caracteristic) {
        this.name={
            first: first,
            last: last
        };
        this.age = age;
        this.gender = gender;
        this.interest = interest;
        this.caracteristic = caracteristic;
     }
   
     bio = () => `${this.name.first} ${this.name.last} is ${this.age}.Is ${this.gender} and likes ${this.interest}`;
     greeting = () => `Hi! I'm ${this.name.first}.`;       
     personality = () => `${this.name.first} is ${this.caracteristic.join(' ')}`; 

}

let people1 = new People(
  'Sandra', 'Stward', 47, 'female',
  ['arts', 'cinema', 'poetry', 'photography', 'arts and crafts', 'painting', 'drawing', 'creative art'],
  ['kind', 'clever', 'sweet', 'empathic', 'emotive', 'hight9 sensitive person']
 );
 
console.log(people1.name)
console.log(people1.bio())
console.log(people1.greeting())
console.log(people1.personality())

 

【讨论】:

    【解决方案2】:

    您可以使用Array.join(' ') 方法。或者Array.reduce 使用字符串作为累加器,并在每次迭代时将当前字符串与累加的字符串连接起来。

    【讨论】:

      【解决方案3】:

      考虑使用Array join

      console.log(['kind', 'clever', 'sweet', 'empathic', 'emotive', 'hight sensitive person'].join(", "))
      

      返回

      “善良、聪明、可爱、善解人意、情绪化、高度敏感的人”

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-06
        相关资源
        最近更新 更多