使用过滤器返回对象中的属性值 [英] use filter to return property values in an object

查看:718
本文介绍了使用过滤器返回对象中的属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图创建一个使用过滤器但不使用for或while循环或foreach函数的函数,该函数将遍历对象数组仅返回其属性值.例如

Trying to make a function that uses filter but not a for or while loop or foreach function, that will loop through an array of objects only to return their property values. For example,

function getShortMessages(messages) {
    return messages.filter(function(obj){
        return obj.message
    });
}

所以如果我打电话

getShortMessages([{message:"bleh"},{message:"blah"}]); 

我应该返回一个数组= ["bleh","blah"] 我只是不确定如何在这些准则下实施过滤器.我也在考虑使用链功能,可能是.map.

I should get a return of an array = ["bleh","blah"] I'm just not sure how to implement filter under these guidelines. Also I was thinking of using a chain function maybe .map.

/////这是整个代码挑战规范//////

//// Here is the entire code challenge specification/////

基本:过滤器 练习4之18

Basic: Filter Exercise 4 of 18

使用Array#filter编写一个名为getShortMessages的函数.

Use Array#filter to write a function called getShortMessages.

getShortMessages接受具有'.message'属性的对象数组,并返回小于<的消息数组.长50个字符.

getShortMessages takes an array of objects with '.message' properties and returns an array of messages that are less than < 50 characters long.

该函数应该返回一个包含消息本身的数组,而不包含它们的对象.

The function should return an array containing the messages themselves, without their containing object.

  • 消息:10到100个随机对象组成的数组,看起来像这样:
{
    message: 'Esse id amet quis eu esse aute officia ipsum.' // random
}

条件

  • 请勿使用任何for/while循环或Array#forEach.
  • 请勿创建任何不必要的功能,例如助手.
  • Conditions

    • Do not use any for/while loops or Array#forEach.
    • Do not create any unnecessary functions e.g. helpers.
      • 尝试链接一些Array方法!
      [ 'Tempor quis esse consequat sunt ea eiusmod.',
        'Id culpa ad proident ad nulla laborum incididunt.',
        'Ullamco in ea et ad anim anim ullamco est.',
        'Est ut irure irure nisi.' ]
      

      资源

      • https://developer.mozilla. org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
      • https://developer.mozilla. org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/map
      • Resources

        • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
        • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
        • function getShortMessages(messages) {
            // SOLUTION GOES HERE
          }
          
          module.exports = getShortMessages
          

          »要再次打印这些说明,请运行:functional-javascript print »要在测试环境中执行程序,请运行:functional-javascript run program.js »要验证您的程序,请运行:functional-javascript verify program.js »要获取帮助,请运行:functional-javascript帮助

          » To print these instructions again, run: functional-javascript print » To execute your program in a test environment, run: functional-javascript run program.js » To verify your program, run: functional-javascript verify program.js » For help run: functional-javascript help

          推荐答案

          当您要获取与预期属性匹配的整个对象时,请使用.filter.当您有很多东西想要对这些东西做一些操作并获得结果时,请使用.map.

          Use .filter when you want to get the whole object(s) that match the expected property or properties. Use .map when you have an array of things and want to do some operation on those things and get the result.

          面临的挑战是获取所有不超过50个字符的消息.因此,您可以使用filter仅获取通过测试的消息,然后使用map仅获取消息文本.

          The challenge is to get all of the messages that are 50 characters or less. So you can use filter to get only the messages that pass that test and then map to get only the message text.

          function getShortMessages(messages) {
            return messages
              .filter(function(obj) {
                return obj.message.length <= 50;
              })
              .map(function(obj) {
                return obj.message;
              });
          }
          

          JSFiddle: http://jsfiddle.net/rbbk65sq/

          JSFiddle: http://jsfiddle.net/rbbk65sq/

          如果输入对象可能没有message属性,则可以使用obj.message && obj.message.length <= 50进行测试,如下所示:

          If it is possible for the input objects to not have a message property, you would want to test for it with obj.message && obj.message.length <= 50 like this:

          function getShortMessages(messages) {
            return messages
              .filter(function(obj) {
                return obj.message && obj.message.length <= 50;
              })
              .map(function(obj) {
                return obj.message;
              });
          }
          

          ES6

          ES6中的相同代码示例:

          ES6

          The same code samples in ES6:

          const getShortMessages = (messages) => messages
            .filter(obj => obj.message.length <= 50)
            .map(obj => obj.message);
          

          如果输入对象可能不具有message属性:

          And if the input objects might not have the message property:

          const getShortMessages = (messages) => messages
            .filter(obj => obj.message && obj.message.length <= 50)
            .map(obj => obj.message);
          

          JSFiddle: http://jsfiddle.net/npfsrwjq/

          JSFiddle: http://jsfiddle.net/npfsrwjq/

          这篇关于使用过滤器返回对象中的属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆