Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

281
Views
use filter to return property values in an object

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
    });
}

so if I call

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

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/////

Basic: Filter Exercise 4 of 18

Task

Use Array#filter to write a function called getShortMessages.

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.

Arguments

  • messages: an Array of 10 to 100 random objects that look something like this:
{
    message: 'Esse id amet quis eu esse aute officia ipsum.' // random
}

Conditions

  • Do not use any for/while loops or Array#forEach.
  • Do not create any unnecessary functions e.g. helpers.

Hint

  • Try chaining some Array methods!

Example

[ '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.' ]

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

Boilerplate

function getShortMessages(messages) {
  // SOLUTION GOES HERE
}

module.exports = getShortMessages

» 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

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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.

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/

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

The same code samples in ES6:

const getShortMessages = (messages) => messages
  .filter(obj => obj.message.length <= 50)
  .map(obj => obj.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/

over 4 years ago · Santiago Trujillo Report

0

Though I realize this thread is super old, I felt it necessary to comment in the event that someone stumbles on it again. I would do it like above just using es6 syntax like this:

objects.filter(obj => obj.key === 'value').map(filteredObj => filteredObj.key);

So the above example would be:

getShortMessages = (messages) => messages.filter(obj => obj.message.length <= 50).map(obj => obj.message);
over 4 years ago · Santiago Trujillo Report

0

With custom return value (Simple ES6 Example);

const customReturnFiltere = (result) => {
    return products.filter((obj) => {
      return obj.stock !== 0;
    })
    .map((finalResult) => {
        return {
          ...finalResult,
          info: 'product will be filtered if stock is 0'
        }
    });
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!