-1

I have data like this:

var a = [Joe,Peter,12345,programmer];
var companies = [xyz,abc,def];
var xyz = [programmer,HR,Tester];
var abc= [HR,Tester];
var def = [programmer,HR,Tester];

I want to create Json data like this dynamically

var json = {
    "FirsName" : "Joe"
    "LastName" : "Peter"
    "Phone"    : "12345"
    "XYZ"      : "Checked"   // if the company has programmer it should give checked 
    "abc"      : "unchecked" // if the company has programmer it should give unchecked 
    "def"      : "checked"
}
Jquey007
  • 65
  • 1
  • 1
  • 6
  • 1
    And what is your question? And what have you tried already? Add any code you've tried to your "question". – Andy Mar 13 '15 at 13:40
  • I believe that I have interpreted your question correctly. See my answer below. Also, your example data is not valid JavaScript and you cannot reference variables by name. – Mr. Polywhirl Mar 13 '15 at 14:12

3 Answers3

1

You cannot create an array called companies that reference variable names* as they do not mean anything to the interpreter. Instead, create a mapping of companies.

var data = [ 'Joe', 'Peter', '12345', 'programmer' ];

var companies = {
  'xyz' : [ 'programmer', 'HR','Tester' ],
  'abc' : [ 'HR', 'Tester' ],
  'def' : [ 'programmer', 'HR,Tester' ]
};

function create(empData) {
  var employee = {
    firstName : empData[0],
    lastName  : empData[1],
    phone     : empData[2]
  };
  
  var ocupation = empData[3];
  for (var company in companies) {
    employee[company] = companies[company].indexOf(ocupation) > -1 ? 'checked' : 'unchecked';
  }
  
  return employee;
}

document.getElementsByTagName('body')[0].innerHTML = JSON.stringify(create(data), undefined, '  ');
body {
    white-space: pre;
    font-family: monospace;
}

Result

{
  "firstName": "Joe",
  "lastName": "Peter",
  "phone": "12345",
  "xyz": "checked",
  "abc": "unchecked",
  "def": "checked"
}

Notes

* If that array contains strings, as I assume that is what you are attempting to do, then this is not possible; but if you are referencing them by their variable reference, and not the string name, then you need to define the variables first.

Not Valid

You cannot reference a variable without scope.

var companies = ["xyz", "abc", "def"];
var xyz = ["Programmer", "HR", "Tester"];
var abc = ["HR", "Tester"];
var def = ["Programmer", "HR", "Tester"];

Not Valid

You cannot reference a variable that has not been defined.

var companies = [xyz, abc, def];
var xyz = ["Programmer", "HR", "Tester"];
var abc = ["HR", "Tester"];
var def = ["Programmer", "HR", "Tester"];

Valid

This creates a 2-dimensional array, but you don't know which company you are referring to, because there is not object-key relationship.

var xyz = ["Programmer", "HR", "Tester"];
var abc = ["HR", "Tester"];
var def = ["Programmer", "HR", "Tester"];
var companies = [xyz, abc, def];
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
  • Hi Mr.Polywhirl I have a question I'm getting those companies list dynamically is there any solution for that? – Jquey007 Mar 13 '15 at 19:28
  • If they were part of an object, then you could use the scope of said object to access them. Technically, that is what I did in my solution. Variables by themselves cannot be explicitly referenced by their "symbol" (The variable name you designated). – Mr. Polywhirl Mar 13 '15 at 20:46
  • You could possibly reference them by the window object, this is bad. Stay away from global scope. Stay as local as possible. Check out closures. Here is a quote from a popular answer in regards to scoping. It seems to explain your situation: [*"All JS objects (which variables are) are available within their scope as named properties of their parent object. Where no explicit parent exists, it is implicitly the window object."*](http://stackoverflow.com/a/724923). – Mr. Polywhirl Mar 13 '15 at 20:53
0

I don't know what you mean exactly but the way I do it is like this:

var myJson = new Object();

myJson.FirsName= "Joe";
myJson.LastName= "Peter";
myJson.Phone= "12345";
myJson.XYZ= true;
myJson.abc= false;
myJson.def= true;

var jsonString= JSON.stringify(obj);

hope it's help!

gon250
  • 3,405
  • 6
  • 44
  • 75
0

Based on your data provided, I think you want to take decision that the array a designation matches with company array(s) values then it's value in JSON object should be true otherwise false.

If right, the following might be helpful:

var a = ["Joe", "Peter", "12345", "Programmer"];
    var companies = ["xyz", "abc", "def"];
    var xyz = ["Programmer", "HR", "Tester"];
    var abc = ["HR", "Tester"];
    var def = ["Programmer", "HR", "Tester"];
    
    var jsonObject = new Object();
    
    jsonObject.FirsName = a[0];
    jsonObject.LastName = a[1];
    jsonObject.Phone = a[2];
    jsonObject.XYZ = xyz.indexOf(a[3]) > -1 ? true : false;
    jsonObject.abc = abc.indexOf(a[3]) > -1 ? true : false;
    jsonObject.def = def.indexOf(a[3]) > -1 ? true : false;;
    
    var jsonString = JSON.stringify(jsonObject);
    document.write(jsonString);
Parkash Kumar
  • 4,710
  • 3
  • 23
  • 39
  • You didn't even use the `companies` variable. I think @Jquey007 needs to rethink their data model. See my response below. – Mr. Polywhirl Mar 13 '15 at 14:19
  • I know, what is the use of company array if other three arrays have been created. This is just to create code-complications, nothing else. – Parkash Kumar Mar 13 '15 at 14:20
  • No, I wasn't saying *you* had to, I was just waiting to see someone *not using it* to validate my point below. – Mr. Polywhirl Mar 13 '15 at 14:22