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

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)

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);

Comments

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.