2

I have this json object. Json key remarks and address is empty. If its empty then json prints undefine when extracting json. But i want replace empty json with - sign. I know I can use:

 if(msg.hasOwnProperty('remarks')== ""){
        alert("A");
   }

But how to loop through all the keys and check if its empty and replace with - rather than checking it individually.

[
{
    "id": "224",
    "booking_id": "1",
    "room_type": null,
    "no_of_rooms_booked": null,
    "check_in_date": "2014-12-23",
    "check_out_date": "2014-12-24",
    "room_id": "93",
    "hotel_id": "9",
    "status": "1",
    "user_id": "15",
    "payment_type": "",
    "payment_status": "",
    "hotelname": "sample hotel",
    "hoteladdress": "sample address",
    "hotelcontact": "056533977",
    "Id": "224",
    "full_name": "Hom Nath Bagale",
    "address": "",
    "occupation": "Student",
    "nationality": "Nepali",
    "contact_no": "9845214140",
    "email": "bhomnath@salyani.com.np",
    "remarks": "",
    "total_amount": "5000",
    "child": "0",
    "adult": "1",
    "verification_code": null,
    "roomname": "sample room2",
    "roomprice": "1.5",
    "roomdescription": "this is for demo",
    "roomimage": "2.jpg"
}]
Homnath Bagale
  • 464
  • 1
  • 6
  • 32

4 Answers4

6

Try this

data.forEach(function (el) {
  Object.keys(el).forEach(function (property) {
    if (el[property] === '') {
      el[property] = '-';
    }
  });
});

Example

Object.keys() - returns an array of a given object's own enumerable properties

Oleksandr T.
  • 76,493
  • 17
  • 173
  • 144
3

Do

  var msg=
    {
        "id": "224",
        "booking_id": "1",
        "room_type": "",
        "roomimage": "2.jpg"
    };

 $.each(msg, function(k, v) {
         if(v===""){
           alert(k + ' is empty' + v);  
           //do actions
         }else{
            alert(k + ' is ' + v);
          }
    });

DEMO

singhakash
  • 7,891
  • 6
  • 31
  • 65
1

You can use the for...in loop to iterate through each property like

for (var key in msg) {
    if (msg.hasOwnProperty(key)) {
        if (msg[key] === '') {
            msg[key] = '-'
        }
    }
}

Since you have used jQuery, you could also use the $.each() method like

$.each(array, function (i, msg) {
    $.each(msg, function (key, value) {
        if (value === '') {
            msg[key] = '-'
        }
    })
});
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
0

Below I have provided 2 functions, first of which can handle your entire json. The second is a much simpler one where you have to pass a specific element from your array (since you know the keys you look for are only available in the first element of the array).

Speciality of both these solutions is that they don't modify (mutate) the original json which you pass to them.

var sampleJson = [{
  "id": "224",
  "booking_id": "1",
  "remarks": "",
  "address": "",
}];

var newJson = getNonEmptyArry(sampleJson);
var newObj = getNonEmptyObj(sampleJson[0]);

console.log('newJson: ' + JSON.stringify(newJson));
console.log('newObj: ' + JSON.stringify(newObj));
console.log('Original Json ' + JSON.stringify(sampleJson));

function getNonEmptyArry(json) {
  var cpyJson = JSON.parse(JSON.stringify(json));
  cpyJson.forEach(obj => {
    Object.keys(obj).forEach(key => {
      if (obj[key] === '') {
        obj[key] = '-';
      }
    });
  });
  return cpyJson;
};

function getNonEmptyObj(obj) {
  var obj = JSON.parse(JSON.stringify(obj));
  Object.keys(obj).forEach(key => {
    if (obj[key] === '') {
      obj[key] = '-';
    }
  });
  return obj;
};
Hamzeen Hameem
  • 2,360
  • 1
  • 27
  • 28