Transform variables in JSON format with JS/jQuery [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 21 days ago.
Improve this question
I have these JS variables:
var var1 = 'Banana';
var var2 = 'Apple';
var var3 = 'Cherry';
How can I transform, them in JSON like this ?
{
var1:"Banana",
var2:"Apple",
var3:"Cherry"
}
Thanks.

JavaScript provides JSON.stringify for serialization in JSON and JSON.parse for reading from JSON.
const json = JSON.stringify({ var1, var2, var3 });

Related

convert a javascript object's value-field to array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
suppose the object is obj ={foo:"bla,bla2,bla3",bar:"bla,bla5"}
I want to convert it to 'obj={foo:["bla","bla2","bla3"],bar:["bla","bla5"]}'
Don't know why you want to do that, but here you can just loop through obj with a for...in loop and split the strings by commas:
const obj = {foo:"bla,bla2,bla3",bar:"bla,bla5"};
for (name in obj) {
obj[name] = obj[name].split(',');
}
console.log(obj)

How to call a function taking parameters in js string template literal? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
say i have a string like this
const myObj = {"far": " bar"}
const s = "hello " + JSON.stringify(myObj)
How to convert it to template literal ?
The same way you put any other expression in a template literal, just wrap it in ${}.
const myObj = {"far": " bar"}
const s = `hello ${JSON.stringify(myObj)}`;
console.log(s);

How to parse or loop this [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have tried everything I can think of to get the data but cannot figure this out.
The data is in a structure like this:
[{"code":1000,"day":"Sunny","night":"Clear","icon":113,"languages":
[{"lang_name":"Arabic","lang_iso":"ar","day_text":"مشمس","night_text":"صافي"}]
}]
I've tried looping, using key:value using the dot notation and bracket notation and cannot get the info.
I'm trying to get to the "languages" so I can parse them for the weather.
var arr = [{"code":1000,"day":"Sunny","night":"Clear","icon":113,"languages":
[{"lang_name":"Arabic","lang_iso":"ar","day_text":"مشمس","night_text":"صافي"}]
}]
arr.forEach(function(obj){ //loop array
obj.languages.forEach(function(language) { //loop languages
console.log(language); //language object
console.log(language.lang_name); //language property
});
});

How do I get length of my Json Object [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Below is my JSON object
{"Decision":[{"recid":"1183","reason":"Approved as Requested","decision":"Approved","approvalamt":"","comment":""},{"recid":"662","reason":"3","decision":"Rejected","approvalamt":"","comment":""},{"recid":"752","reason":"Approved People Resources; But No Funding Approved","decision":"Approved","approvalamt":"","comment":""}]}
How do I get length of this.In this example it should return me 3.
My Object name is DecisionObj which have above listed elements.
var string = '{"Decision":[{"recid":"1183","reason":"Approved as Requested","decision":"Approved","approvalamt":"","comment":""},{"recid":"662","reason":"3","decision":"Rejected","approvalamt":"","comment":""},{"recid":"752","reason":"Approved People Resources; But No Funding Approved","decision":"Approved","approvalamt":"","comment":""}]}';
var object = JSON.parse(string);
alert(object.Decision.length);
Arrays have length property. You can use that.
var length = DecisionObj.length;

regular expression in javascript checking for "asdasd<something>asdsada" [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
how would I check for string
<something> in var string = "some chars <something> somechars"
You can use String.match():
var res = string.match(/<something>/g); // ["<something>"]
If there is no match, the value of res will be null. See example on JSFiddle.

Categories