-1

I have a Object that contain objetcs like this.Object {MAILING ADDRESS: "P O BOX 59", APN: "066-102-11-1"} . Now I need data without empty Object. Like I get output like this Output

Object {MAILING ADDRESS: "P O BOX 59", APN: "066-102-11-1"}
Object {MAILING ADDRESS: "", APN: ""}
Object {MAILING ADDRESS: "P O BOX 3", APN: "066-105-11-1"}
Object {MAILING ADDRESS: "", APN: ""} 

So in this case I dont want to get 2nd and 4th object. And in case of 100 I dont want to get 2,4,6,8..100 index . Because output after one time is repeating and I have to remove this. ScreenShot of output I getting

//Code How I am creating this
ExportTool.prototype.click = function(value) {
  var lidtest = mapport.panels.info.current.selection.features.mem;

  if (value != undefined) {
    var lid = lidtest[value].attributes.layer_id;
  } else {
    var lid = lidtest[0].attributes.layer_id;
  }

  if (!lid) return;
  var tbody;
  var thead;
  tbody = $('<tbody></tbody>');
  thead = $('<thead></thead>');
  self = this;

  // Reset
  this.tblHeader = [];
  this.tblData = [];
  this.labelData = [];

  // thead.children().remove();
  // tbody.children().remove();
  //var tbody;

  var layer = mapport.layer(lid);
  var tr = $('<tr></tr>');

  layer.fields.each(function(field) {
    tr.append($('<th ></th>').text(field.name));
    // Table heading for the PDF
    if (self.availableForTable.indexOf(field.name.toUpperCase()) != -1)
      self.tblHeader.push(field.name);
  });

  tbody.append(tr);

  var features = mapport.panels.info.current.features();
  for (var i = 0; i < features.length; ++i) {
    if (features[i].geometry != null) {
      var data = features[i].attributes.data,
        row_data = [],
        row_field, obj_field = {};

      tr = $('<tr></tr>');

      layer.fields.each(function(field) {
        var field_name = field.name.toUpperCase();
        var td = $('<td></td>');
        if (data[field.id] != null) td.text(data[field.id]);
        tr.append(td);

        if (self.availableForTable.indexOf(field_name) != -1) {
          row_field = (data[field.id] != null) ? data[field.id] : '';
          row_data.push(row_field);
          obj_field[field_name] = row_field;
        }
      });

      row_data = row_data.filter(function(entry) {
        return /\S/.test(entry);
      });

      obj_field = JSON.parse(obj_field);
      console.log(obj_field);
      // Table Data for the PDF
      this.tblData.push(row_data);
      // Table Data for the PDF
      this.labelData.push(obj_field);
      tbody.append(tr);
      $('#table_multi_layers').append(tbody);
    }
  }
}
13
  • 3
    Could you post the full data structure. This looks more like an array of objects. Commented May 9, 2017 at 8:42
  • 1
    Is this an array of objects? Commented May 9, 2017 at 8:42
  • How does you base object contain these objects, are they in an array ({addresses:[{...},{...}]}) or are the properties of the object ({address1:{...},address2:{...}})? Commented May 9, 2017 at 8:42
  • it looks like you are logging lot of objects. make it an array and then filter them Commented May 9, 2017 at 8:43
  • @RoryMcCrossan creating like this obj_field = {};. This result which I showed is console.log(obj_field ); Commented May 9, 2017 at 8:43

1 Answer 1

0

You can use filter option like this. You can change your object to array and apply filter.

var someArray = $.map(obj_field, function(value, index) {
    return [value];
});

someArray = [{
    MAILINGADDRESS: "P O BOX 59",
    APN: "066-102-11-1"
  },
  {
    MAILINGADDRESS: "",
    APN: ""
  },
  {
    MAILINGADDRESS: "P O BOX 59",
    APN: "066-102-11-1"
  }, {
    MAILINGADDRESS: "",
    APN: ""
  }
];
result = someArray.filter(function(el) {
  return el.APN !== "";
});

console.log(JSON.stringify(result, null, ' '));

Sign up to request clarification or add additional context in comments.

3 Comments

obj_field.filter is not a function that error I get . Because its not array @Shree
what is obj_field ??
is of Object type that contain objects. You can see output snapshot which I have attached , And it is result of console.log(obj_field);

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.