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
2 Answers 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
Arun P Johny
389k68 gold badges533 silver badges532 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Anoop Chandran
Thank you Arun P Johny.. :)
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
SharpEdge
1,7621 gold badge10 silver badges20 bronze badges
Comments
lang-js