Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8465615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:06:05+00:00 2026-06-10T15:06:05+00:00

I have an array of objects where I wanna only save a sub object

  • 0

I have an array of objects where I wanna only save a sub object when it pass a condition. At the moment I first use map and then filter with the result:

   var a = _.filter(
    _.map(activities.toJSON(), function (activity) {
    if (activity.verb !== 'test') {
      return activity.object;
    }
  }), function(b){return b;});

This seems a bit odd. Is there a better way to do this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T15:06:07+00:00Added an answer on June 10, 2026 at 3:06 pm

    Any time you start doing things like _.f(_.g(...)), you should think about using _.chain. You could do something like this:

    var a = _.chain(activities.toJSON())
             .select(function(a) { return a.verb !== 'test' })
             .map(function(a) { return a.object })
             .value();
    

    I’ve also adjusted your logic to simplify things, there’s no need to do the same test twice. You can make this sort of thing quite readable if you put your functions into variables:

    var not_tests   = function(a) { return a.verb !== 'test' };
    var extract_obj = function(a) { return a.object };
    var results     = _.chain(activities.toJSON())
                       .select(not_tests)
                       .map(extract_obj)
                       .value();
    

    Demo: http://jsfiddle.net/ambiguous/EQfuH/

    There will be a fair bit of copying with this approach so you might want to just go old-school and use a loop:

    var json = activities.toJSON();
    var a    = [ ];
    for(var i = 0; i < json.length; ++i)
        if(json[i].verb !== 'test')
            a.push(json[i].object);
    

    There’s nothing at all wrong with loops in JavaScript.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of objects and I want to add another object just
I have an array of objects, and i use following code to get in
I have an array of objects . Each object has an attribute we'll call
I have an array of objects and I want to pass them into a
I have array of objects with created images (Object.Image), i want to show images
I have array of objects. I want to write method wich i will use
I have an array of objects named x (that is Object[] x). When I
I have an array of objects in Javascript. Can I change the object values
I have an array of objects. Each object has a color attribute which could
I have an array with objects, I want to save each one of them

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.