使用对象属性的值制作字符串的问题

时间:2019-07-02 07:43:42

标签: javascript arrays object javascript-objects

我在创建一个新属性时遇到问题,该新属性包含使用现有属性值的字符串。你能让我知道为什么我不能使它工作吗?

 function greetDevelopers(list) {
  for(let i=0 ; i< list.length ; i++ ){
  return 'Hi '+list[i].firstName +', what do you like the most about 
 '+list[i].language +'?'
  }
 }

当我编写这样的代码向每个对象中输入新属性时,它始终显示第一个对象的结果。

但是,如果我尝试过

 function greetDevelopers(list) {
  for(let i=0 ; i< list.length ; i++ ){
  return 'Hi '+list[1].firstName +', what do you like the most about 
 '+list[1].language +'?'
  }
 }

它在对象中显示正确的值。

https://www.codewars.com/kata/coding-meetup-number-2-higher-order-functions-series-greet-developers/train/javascript

您的任务是返回一个数组,其中每个对象将具有一个带有以下字符串值的“ greeting”新属性:

您好<这里的名字>,您最喜欢<这里的语言>什么?

function greetDevelopers(list) {
 for(let i=0 ; i< list.length ; i++ ){
 return 'Hi '+list[i].firstName +', what do you like the most about 
'+list[i].language +'?'
 }
}

var list1 = [
  { firstName: 'Sofia', lastName: 'I.', country: 'Argentina', continent: 
'Americas', age: 35, language: 'Java' },
  { firstName: 'Lukas', lastName: 'X.', country: 'Croatia', continent: 
'Europe', age: 35, language: 'Python' },
  { firstName: 'Madison', lastName: 'U.', country: 'United States', 
continent: 'Americas', age: 32, language: 'Ruby' } 
];
Test.assertDeepEquals(greetDevelopers(list1), answer);

[
  { firstName: 'Sofia', lastName: 'I.', country: 'Argentina', continent: 
'Americas', age: 35, language: 'Java',
    greeting: 'Hi Sofia, what do you like the most about Java?'
  },
  { firstName: 'Lukas', lastName: 'X.', country: 'Croatia', continent: 
'Europe', age: 35, language: 'Python',
    greeting: 'Hi Lukas, what do you like the most about Python?'
  },
  { firstName: 'Madison', lastName: 'U.', country: 'United States', 
continent: 'Americas', age: 32, language: 'Ruby',
    greeting: 'Hi Madison, what do you like the most about Ruby?'
  } 
];

我想要这样。但是我的代码不起作用。你能帮忙吗? 谢谢!

5 个答案:

答案 0 :(得分:0)

返回循环中的字符串。每当您返回时,都意味着要停止所有操作并返回给定的值,这很好,例如,当您在循环中搜索特定的元素然后找到了它时,因为这样做没有意义。完成循环。

但是,在我看来,您的意思是为每个用户返回一个字符串

为此,您可以执行以下操作:

function greetDevelopers(list) {
  let greetings = new Array();
  for(let i=0 ; i< list.length ; i++ ){
    greetings.push('Hi '+list[i].firstName +', what do you like the most about '+list[i].language +'?');
  }
  return greetings;
}

console.log(greetDevelopers([{firstName: 'Jon', language: 'Java'}, {firstName: 'Peter', language: 'English'}]));

或者,在更多的高级浏览器中,您还可以执行以下操作,从而为您提供更多基于迭代器的控制方法:

    function* greetDevelopers(list) {
      for(let i=0 ; i< list.length ; i++ ){
        yield 'Hi '+list[i].firstName +', what do you like the most about '+list[i].language +'?';
      }
    }

const greetingsIterator = greetDevelopers([{firstName: 'Jon', language: 'Java'}, {firstName: 'Peter', language: 'English'}]);

console.log(greetingsIterator.next().value);
console.log(greetingsIterator.next().value);

有关yield和迭代器协议的更多详细信息,请参见here

答案 1 :(得分:0)

您需要在for循环中调用函数。您将返回列表的第一个结果,而不是让它有循环的机会。

function greetDevelopers(developer) {
    return 'Hi '+developer.firstName +', what do you like the most about '+developer.language +'?'
 }

for(let i=0 ; i< list.length ; i++ ){
  greetDevelopers(list[i]);
}

答案 2 :(得分:0)

您需要使用正在构造的字符串向greeting中的每个对象添加一个list属性。

function greetDevelopers(list) {
  for (let i = 0; i < list.length; i++) {
    list[i].greeting = 'Hi ' + list[i].firstName + ', what do you like the most about ' + list[i].language + ' ? ';
  }
  return list;
}

var list1 = [{firstName:'Sofia',lastName:'I.',country:'Argentina',continent:'Americas',age:35,language:'Java'},{firstName:'Lukas',lastName:'X.',country:'Croatia',continent:'Europe',age:35,language:'Python'},{firstName:'Madison',lastName:'U.',country:'United States',continent:'Americas',age:32,language:'Ruby'}]

console.log(greetDevelopers(list1));
.as-console-wrapper { max-height: 100% !important; top: auto; }

答案 3 :(得分:0)

您可以使用Array#map返回一个对象数组,其中firstNamelanguage的{​​{3}}和带有更改值的所需字符串的destructuring assignment

function greetDevelopers(list) {
    return list.map(({ firstName, language }) =>
        ({ geeting: `Hi ${firstName}, what do you like the most about ${language}?` }));
}

var list1 = [{ firstName: 'Sofia', lastName: 'I.', country: 'Argentina', continent: 'Americas', age: 35, language: 'Java' }, { firstName: 'Lukas', lastName: 'X.', country: 'Croatia', continent: 'Europe', age: 35, language: 'Python' }, { firstName: 'Madison', lastName: 'U.', country: 'United States', continent: 'Americas', age: 32, language: 'Ruby' }];

console.log(greetDevelopers(list1));

答案 4 :(得分:0)

当需要从另一个数组创建一个数组时,通常可以使用map函数。 map函数获取一个数组的每个条目并返回一个新数组。对于您的情况,我会那样做:

const list1 = [
  { firstName: 'Sofia', lastName: 'I.', country: 'Argentina', continent: 
'Americas', age: 35, language: 'Java',
    //greeting: 'Hi Sofia, what do you like the most about Java?'
  },
  { firstName: 'Lukas', lastName: 'X.', country: 'Croatia', continent: 
'Europe', age: 35, language: 'Python',
    //greeting: 'Hi Lukas, what do you like the most about Python?'
  },
  { firstName: 'Madison', lastName: 'U.', country: 'United States', 
continent: 'Americas', age: 32, language: 'Ruby',
    //greeting: 'Hi Madison, what do you like the most about Ruby?'
  } 
];

const greetDevelopers = list1.map((item) => {
    return `Hi ${item.firstName}, what do you like the most about ${item.language}?`
});

console.log(greetDevelopers);

或者,如果您确实要将属性添加到现有对象中,则:

let list1 = [
  { firstName: 'Sofia', lastName: 'I.', country: 'Argentina', continent: 
'Americas', age: 35, language: 'Java',
    // greeting: 'Hi Sofia, what do you like the most about Java?'
  },
  { firstName: 'Lukas', lastName: 'X.', country: 'Croatia', continent: 
'Europe', age: 35, language: 'Python',
    // greeting: 'Hi Lukas, what do you like the most about Python?'
  },
  { firstName: 'Madison', lastName: 'U.', country: 'United States', 
continent: 'Americas', age: 32, language: 'Ruby',
    // greeting: 'Hi Madison, what do you like the most about Ruby?'
  } 
];

list1.forEach((item) => {
    item.greeting = `Hi ${item.firstName}, what do you like the most about ${item.language}?`
});

console.log(list1);