Click here to Skip to main content
15,902,635 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
var obj = {
LOBID: 1
LOBName: "Claim Processing"
}
var obj = {
IndustryID: 1
IndustryeName: "Health"
}

function ObjectToString(obj, ID, Name) {
debugger
if (obj != null && obj.length > 0) {
var result = '';
var result1 = '';
for (i = 0; i < obj.length; i++) {
//getting error the below step "obj[i].ID".
result = obj[i].ID; // ID = "LOBID"
result1 = obj[i].Name; // Name = "LOBName"
}
return result + "|" + result1;
}
}

var finalLOB = ObjectToString(obj,'LOBID','LOBName');

var finalIndustry = ObjectToString(obj,'IndustryID','IndustryeName');

What I have tried:

I have tried the above code but getting error on this "result = obj[i].ID;"
please suggest how to pass dynamic call for ObjectToString function.
Posted
Updated 28-Jan-19 22:55pm

You can't use Obj[i].ID as it doesn't exist.

You could do:

var obj = {
ID: 1
Name: "Claim Processing"
}

LOB = new [obj];

var obj2 = {
ID: 1
Name: "Health"
}

Industry - new [obj2];

And store them in separate arrays as from what your code suggests, you're treating them as independant data sets anyway. Else I'd suggest a third parameter such as:

var obj = {
ID: 1
Name: "Claim Processing"
Type: "LOB"
}

LOB = new [obj];

var obj2 = {
ID: 1
Name: "Health"
Type: "Industry"
}

And do your queries against that third parameter.
 
Share this answer
 
v2
Comments
venkatesh (chennai) 29-Jan-19 4:37am    
thanks for reply.
If you are trying to create a generic method for object to string by concatenating all the key values in your js object then you can do the following

function ObjectToString(inputobj){
var output=" ";
var jsonkeys = (Object.keys(inputobj));
for(i=0;i<jsonkeys.length;i++){
output=output+""+inputobj[jsonkeys[i]];
}
return output;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900