使用地图时如何创建和新建对象?

时间:2018-10-16 08:33:14

标签: javascript dictionary functional-programming

关于以下问题,我必须从list1创建list2,因此我应用了效果很好的解决方案1。但是,我必须将其单独进行单元测试。更改后,我无法使其像解决方案2一样。如果我打印返回的值,则表示数组的3个元素未定义。有人对这个问题有什么建议吗?我已经尽力了,但仍然无法解决。

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' } 
];

var list2 = [
  { 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?'
  } 
];

解决方案1 ​​

let greetings1 = list1.map(person => {
  return Object.assign(
    { firstName: person.firstName },
    { lastName: person.lastName },
    { country: person.country },
    { continent: person.continent },
    { age: person.age },
    { language: person.language },
    {
      greeting: `Hi ${person.firstName}, what do you like the most about ${
        person.language
      }?`
    }
  );
});

解决方案2

function greetDevelopers(list1) {
  const greetings = list1.map(person => {
    Object.assign(
      { firstName: person.firstName },
      { lastName: person.lastName },
      { country: person.country },
      { continent: person.continent },
      { age: person.age },
      { language: person.language },
      {
        greeting: `Hi ${person.firstName}, what do you like the most about ${
          person.language
        }?`
      }
    );
  });
  return greetings;
}

5 个答案:

答案 0 :(得分:0)

function greetDevelopers(list1) {
  const greetings = list1.map(person => {
    return Object.assign( // add return before Object.assign
      { firstName: person.firstName },
      { lastName: person.lastName },
      { country: person.country },
      { continent: person.continent },
      { age: person.age },
      { language: person.language },
      {
        greeting: `Hi ${person.firstName}, what do you like the most about ${
          person.language
        }?`
      }
    );
  });
  return greetings;
}

答案 1 :(得分:0)

您可以通过复制旧属性来获取对象并仅添加新属性。

let greetings1 = list1.map(person => Object.assign(
    {},
    person,
    { greeting: `Hi ${person.firstName}, what do you like the most about ${person.language}?`}
));

解决方案2不会返回undefied以外的其他值,因为您没有返回创建的对象。

function greetDevelopers(list1) {
    const greetings = list1.map(person => {
        return Object.assign(
        //^^^^
            // ...
        );
   });
   return greetings;
}

答案 2 :(得分:0)

解决方案2不返回Object.assign,因此返回undefined。此外,您可以简化代码以使其遵循

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' } 
];

function greetDevelopers(list1) {
  return list1.map(o => ({...o, greeting: `Hi ${o.firstName}, what do you like the most about ${o.language}?`}));
}
console.log(greetDevelopers(list1));

答案 3 :(得分:0)

我猜您正在处理数据结构和更改数据结构的功能,这意味着Abstract Data Types,而这主要是classes的同义词。在javascript中,可以使用三种主要方法来处理类或对象:

您还在进行单元测试,因此需要在应用程序代码和测试代码之间清楚地分开。为了简单起见,在下面的代码中,我将使用原型。

// APPLICATION CODE
function Person(data) {
  Object.assign(this,data);
  };
  
Person.prototype.greet = function() {
  return `Hi ${this.firstName}, what do you like the most about ${this.language}`;
};

// TESTING DATA
var inputList = [
  { 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' } 
];

// TESTING CODE
const createAndGreet = (data) => {
  const p = new Person(data);
  data.greeting = p.greet(); 
  return data;
};

console.log(inputList.map(createAndGreet));

答案 4 :(得分:0)

这是一种实用的方法:

const input = [{
    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'
  }
]

// objOf :: String -> Any -> Object
const objOf = prop => x => ({ [prop]: x })

// concatObj :: Object -> Object -> Object
const concatObj = x => y => ({ ...x, ...y })

// pipe :: Array (a -> Any) -> a -> b
const pipe = xs => x => xs.reduce((x, f) => f (x), x)

// greeting :: { firstName::String, language::String } -> String
const greeting = ({
  firstName,
  language
}) => `Hi ${firstName}, what do you like the most about ${language}?`

// appendGreeting :: Object -> Object
const appendGreeting = o => pipe ([ 
  greeting, // (1) Generates the greeting
  objOf ('greeting'), // (2) Creates an object owning greeting property
  concatObj (o) // (3) Concats the input object with the "greeting" one
]) (o)

const output = input.map (appendGreeting)

console.log (output)