1

I want to create an object in this format dynamically

var query = {"where":{"lang":"en","category":"welcome,common"}};

I have these objects

console.log(name);
console.log(jsonObj);

Console output

Array[2]
0: "welcome"
1: "common"
Object
 language: "in" 
 location: "location"
 __proto__: Object

I am trying this code..but it is not correct.

for(i in name){
 var categoryVal = name[0]+","+name[1]; // this could be more than two..how to loop..??
}
var query = {"where":{"lang": jsonObj.language , "category":categoryVal } };

Please help

asked May 21, 2015 at 6:51

2 Answers 2

2

Since name is an array, you could use Array.join()

var name = ['welcome', 'common'],
 jsonObj = {
 language: "in",
 location: "location"
 };
var query = {
 "where": {
 "lang": jsonObj.language,
 "category": name.join(',')
 }
};
console.log(query)

answered May 21, 2015 at 6:53
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Arun P Johny.. :)
0

Here we go

var arrObj = ["welcome", "common"];
var jsonObj = {
 location:"location",
 language:"in"
 };
function createObj(arrayObj, jsonObj) {
 var newObj = {
 where:{
 category:arrayObj.join(),
 lang: jsonObj.language
 }
 };
 return newObj;
}
var myObj = createObj(arrObj, jsonObj);
console.log(myObj);

answered May 21, 2015 at 6:57

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.