1

I would like to create json string based on datatable rows. Source below works well , but sometimes i dont have AddressDetails object and I want to not set this. How to add condition to this code ( if selectedRow.DeliveryAddressDetails=null not set DeliveryAddressDetails )

var returnItems=[];
                data.each(function() {
                    var rowIndex = $dataTable.fnGetPosition(this);
                    var selectedRow = $dataTable.fnGetData(rowIndex);
                    returnItems.push(
                        {
                            DeliveryAddress: selectedRow.DeliveryAddress,
                            Extension: selectedRow.Extension,
                            IsCourier: selectedRow.IsCourier,
                            Quantity: selectedRow.Quantity,
                            DeliveryAddressDetails: {
                                AddressDestinationDescription: selectedRow.DeliveryAddressDetails.AddressDestinationDescription,
                                AddressDestinationDetailId: selectedRow.DeliveryAddressDetails.AddressDestinationDetailId,
                                AddressDestinationName: selectedRow.DeliveryAddressDetails.AddressDestinationName,
                                AddressLine1: selectedRow.DeliveryAddressDetails.AddressLine1,
                                AddressLine2: selectedRow.DeliveryAddressDetails.AddressLine2,
                                AddressLine3: selectedRow.DeliveryAddressDetails.AddressLine3,
                                CityName: selectedRow.DeliveryAddressDetails.CityName,
                                PhoneNumber: selectedRow.DeliveryAddressDetails.PhoneNumber,
                                ZipCode: selectedRow.DeliveryAddressDetails.ZipCode
                            }
                        });
                });
        $("#myJson").val(JSON.stringify(returnItems));

thnx for help

2
  • if(selectedRow.DeliveryAddressDetails != null || selectedRow.DeliveryAddressDetails != undefine) { // push into array} Commented Oct 19, 2016 at 16:57
  • Create empty entity for returnItems and use jquery extend for push object is easy way to do this. Commented Oct 19, 2016 at 17:12

2 Answers 2

1

You have to create the object first, and then add that property conditionally

var returnItems = [];
data.each(function() {
    var rowIndex = $dataTable.fnGetPosition(this);
    var selectedRow = $dataTable.fnGetData(rowIndex);
    var obj = {
        DeliveryAddress: selectedRow.DeliveryAddress,
        Extension: selectedRow.Extension,
        IsCourier: selectedRow.IsCourier,
        Quantity: selectedRow.Quantity
    }
    if ( DeliveryAddressDetails !== null ) { // or typeof ** == "object"
        obj.DeliveryAddressDetails = {
            AddressDestinationDescription: selectedRow.DeliveryAddressDetails.AddressDestinationDescription,
            AddressDestinationDetailId: selectedRow.DeliveryAddressDetails.AddressDestinationDetailId,
            AddressDestinationName: selectedRow.DeliveryAddressDetails.AddressDestinationName,
            AddressLine1: selectedRow.DeliveryAddressDetails.AddressLine1,
            AddressLine2: selectedRow.DeliveryAddressDetails.AddressLine2,
            AddressLine3: selectedRow.DeliveryAddressDetails.AddressLine3,
            CityName: selectedRow.DeliveryAddressDetails.CityName,
            PhoneNumber: selectedRow.DeliveryAddressDetails.PhoneNumber,
            ZipCode: selectedRow.DeliveryAddressDetails.ZipCode
        }
    }
    returnItems.push();
});

$("#myJson").val(JSON.stringify(returnItems));
Sign up to request clarification or add additional context in comments.

Comments

0

If you want to conditionally set fields in an object, you'll have to add it after the initial creation.

var objToPush = { /* obj creation */ };
conditionToTest && (objToPush[someKey] = someValue);

The conditionToTest && doSomething pattern will only trigger if the condition is true. It's equivalent to:

var objToPush = { /* obj creation */ };

if (conditionToTest)
    (objToPush[someKey] = someValue);

but a bit more concise.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.