1

I need to get right listbox columns data from json

enter image description here

My json is as follows:

[
  {
    "CustName": "Matt Harrison",
    "name": "Steve Rucci",
    "functionRoom": "Living Room",
    "bookingId": "659",
    "bookingName": "Matt Booking for Lost Business Report Sept 11",
    "postAs": ""
  }
]

I applied a customized function

copyObjectProps(source, keys) {
   let newObject = {}
   keys.forEach(function(key) {
     newObject[key] = source[key]
   })
   return newObject
}

on button click First I prepare list of Columns

$("#button").click(function(){
var s='';
$("Listbox > option").each(function() {
s+="'"+this.text+"',";
});
s=s.replace(/,+$/,'');
objBookingReports.RS_FN_FunctionList=s;
}) 

then this list goes to copyObjectProps() function and create filtered json object

var filteredparam1 = new Array();
var x =objBookingReports.RS_FN_FunctionList;

// x value is "'bookingId','bookingName'"

x = x.replace(/^"\?/, '');
x=x.replace(/"+$/,'');
var y=[x];
$.each(jsonData, function(key, value){
filteredparam1.push(objBookingReports.copyObjectProps(value,y))
});
console.log(filteredparam1)
}

the problem is here in formatiing . when i place column list in y=[x]; .it is not retuning json data with selected columns . but if i tried tried to place hardcore value like

var y=['bookingId','bookingName'];

then it is showing me results . please suggest how can i format my column list correctly ?

Neeraj Verma
  • 2,174
  • 6
  • 30
  • 51

1 Answers1

2

Try like this:

var filteredparam1 = new Array();
var x =objBookingReports.RS_FN_FunctionList;

// x value is "'bookingId','bookingName'"


// x = x.replace(/^"\?/, '');
// x=x.replace(/"+$/,'');
var y = x.replace(/["']/g, "").split(',')  // var y=[x];
$.each(jsonData, function(key, value){
    filteredparam1.push(objBookingReports.copyObjectProps(value,y))
});
console.log(filteredparam1)
}

It seems like that your y variable became a array of just one element: "'bookingId','bookingName'"; just try to replace the single quotes with empty space from the variable x and split by the comma.

Here a solution to replace the single and double quotes.

toom501
  • 324
  • 1
  • 3
  • 15