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 8789529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:25:58+00:00 2026-06-13T22:25:58+00:00

I am using jQuery , Backbone (and underscore ) and RequireJS . I have

  • 0

I am using jQuery, Backbone (and underscore) and RequireJS.

I have this code:

var products = _.map(items, function(item) {
    return new ProductModel({
        title: item.product.title,
        description: item.product.description,
        link: item.product.link,
        'image-src': item.product.images[0].link,
        'image-height': item.product.images[0].height,
        'image-width': item.product.images[0].width
    });
});

Unfortunately, everything might be null. By everything I mean that item, item.product, item.product.images, item.product.images[0] and so on might be nulls.

I am looking for something like “coalesce” that:

  1. will know how to handle nulls and will not throw exceptions for item.product if item is null.
  2. will allow me to provide a default value if there was a null.

Is there anything like that?

  • 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-13T22:25:59+00:00Added an answer on June 13, 2026 at 10:25 pm

    You can use boolean && and rely on short-circuiting:

    ...
    'image_src': item && item.product && item.product.images && item.product.images[0] && item.product.images[0].link,
    'image_height': item && item.product && item.product.images && item.product.images[0] && item.product.images[0].height,
    ...
    

    More likely, you don’t want to perform the same test across so many lines. I would store your return value with its various attributes set to some sane default and then conditionally add the real values if your item.product.images[0] is non-null:

    var returnObject = { /* default values here */ }, product, image;
    
    if (product = item && item.product) {
        returnObject.title = product.title,
        returnObject.description = product.description,
        returnObject.link = product.link
    };
    
    if (image = product && product.images && product.images[0]) {
        returnObject['image-src'] = image.link;
        returnObject['image-height'] = image.height;
        returnObject['image-width'] = image.width;
    }
    
    return returnObject;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing application using backbone.js & jquery. I have following code in model: runReport:
Regarding this entry Loading Backbone and Underscore using RequireJS it is quite clear to
I'm using backbone.js, underscore.js and jQuery. I took the following sample code from http://jsfiddle.net/thomas/C9wew/6/
I'm building a single page, offline html5 web application using jquery mobile, backbone, underscore
I have a website based on Backbone, Underscore and jQuery. It works fine in
This may be a general javascript or jQuery question- I'm using backbone.js and I'd
I have struggled with this for quite a long time. I'm using Backbone.js along
I'm using Backbone.js and jQuery-mobile. jQuery-mobile routing is disabled and I'm using the lib
using jquery's .post i send do my php code an object that with var_dump
Using Jquery , I have an array result [<a href=><img src=image1></a>,<a href=><img src=image2></a>] if

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.