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 ?

3
  • Can you please update your question with the output of the variable x after this line: var x =objBookingReports.RS_FN_FunctionList;? Commented Oct 1, 2018 at 11:18
  • this is giving me output = "'bookingId','bookingName'" Commented Oct 1, 2018 at 11:21
  • Make online code snippet and you will get fast answer! Commented Oct 1, 2018 at 11:30

1 Answer 1

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.

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

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.