1

Ok. I want to create a JSON array dynamically. Case is:

Lets say, I have array:

var arrayCompanyUsers = ["dealers", "builders"];

From the above array, I want to generate another array as below:

[
    "dealers" : {
                  "RequestQuote" : false,
                  "PlaceOrder" : false,
                 },
    "builder" : {
                  "RequestQuote" : false,
                  "PlaceOrder" : false,
                 }
]

Two questions:

  1. how to generate resultant array ?
  2. can i access the properties as: dealers.RequestQuote ?
4
  • You mean, you want an Object from that array? Commented Feb 19, 2015 at 10:02
  • [ "dealers" : { "RequestQuote" : false, "PlaceOrder" : false, }, "builder" : { "RequestQuote" : false, "PlaceOrder" : false, } ] would not be a array.Array does not have key-value pair. You probably want to have Array of Objects like [ {"dealers" : { "RequestQuote" : false, "PlaceOrder" : false, }}, {"builder" : { "RequestQuote" : false, "PlaceOrder" : false, }} ] Commented Feb 19, 2015 at 10:03
  • Yes ofcourse. But my resultant array will contain objects whose names will be assigned from arrayCompanyUsers Commented Feb 19, 2015 at 10:06
  • All of you were right. Thanks for you all. You spent time to answer me. Collectively, I have upvoted all of you as you were right. Commented Feb 19, 2015 at 10:36

3 Answers 3

2

You can do this with the following snipped:

var arrayCompanyUsers = ['dealers', 'builders'];
var target = {};
for (var i = 0; i < arrayCompanyUsers.length; i++){
    var user = arrayCompanyUsers[i];
    target[user] = {
        'RequestQuote' : false,
        'PlaceOrder' : false,
    };
}
console.log(target);

And yes, you should be able to access the properties with dealers.RequestQuote

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

Comments

1

You could use Array.prototype.map to create an array of objects:

var output = arrayCompanyUsers.map(function(key) {
  var record = {};
  record[key] =  {
    RequestQuote : false,
    PlaceOrder : false,
  }

  record data;
})

To get RequestQuote for the "dealers" record:

function getValue(records, name, value) {
  var matches = records.filter(function(record) {
      return record[name] !== undefined;
    })

    if(matches.length > 0) {
      return matches[0][name][value];
    }
}

console.log(getValue(output, 'dealers', 'RequestQuote'));
// -> false

As an aside, your data would be easier to work with if you used the format:

{
  name: "dealers",
  properties: {
    RequestQuote : false,
    PlaceOrder : false,
  }
}

Comments

1

see this : http://jsfiddle.net/zL5x6xxm/3/

 var arrayCompanyUsers = ["dealers", "builders"];
var result=[];
for(i=0;i<arrayCompanyUsers.length;i++)
{var x=arrayCompanyUsers[i]
    result.push('{'+ x+':{  "RequestQuote" : false, "PlaceOrder" : false, }}');  
}

console.log(result);

4 Comments

When I tried your solution with some modifiction, I got [{"x":{"RequestQuote":false,"PlaceOrder":false}},{"x":{"RequestQuote":false,"PlaceOrder":false}}]
I did console.log(JSON.stringify(result)); to see the output
@ChintanSoni create a fiddle of it, what i wrote is according to the requirement you asked in the question
Yes. Just a little correction with your answer. I wanted "dealers" , "builders" instead of "x"

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.