How to convert this json alphabets: {"a","b"} into array of strings [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 2 days ago.
Improve this question
I want to convert the object into arrays but these objects doesnt have key value pairs just values c:{"A","B"}
I was actually getting values from backend in this format alphabets:{"a","b"}. I want assign these values in an array and pass it to the respective variable. so that i can map it and select options which are in the array. In this case a & b

{"a","b"} is not valid JSON so you'd have to parse this yourself. One simple method could consist in replacing the curly braces with square brackets then parse it as JSON:
const value = '{"A", "B"}';
const array = JSON.parse(value.replace('{', '[').replace('}', ']'));
console.log(array);

Related

How to cast a String as an Array then dedupe Array in Java? [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 10 months ago.
This post was edited and submitted for review 10 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
The core focus of this question is to manipulate a string with comma delimiters and duplicate entries in Java so that it returns a deduped, comma delimited string (all in one line and using native classes).
A.) Cast a comma delimited string as an Array,
B.) Dedupe that Array and then convert it back to a string
C.) Check against that newly deduped string to see how many unique entries there were, such that 1 commas means that there were 2 original unique entries.
D.) All of the above must be condensed into one line if possible. (If not, at least A and B is one line and C is its own line.
The following is a reference/picture of what is outlined above using JavaScript and jQuery. It is only anecdotal.
Consider in jQuery the following code to convert a string into an array and then dedupe said array by first converting it to a string and then cast it as a array again in order to leverage a powerful $.unique() jQuery function.
var daysProxyArray = "Monday,Monday,Friday,Friday".split(',');
daysProxyArray = $.unique(daysProxyArray.toString().split(','));
console.log(daysProxyArray.toString());
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Does anyone know how to do the equivalent in Java in one line where the end result is a string that I can count how many times a comma appears? Such that, if 2 commas appear, I will know that 3 unique days were in the original string.
Arrays.stream("Monday,Monday,Friday,Friday".split(","))
.distinct()
.collect(Collectors.joining(","));
If you want to count the amount of commas the following code should do the trick (in Java 8+):
String text = "Monday,Monday,Friday,Friday";
long numCommas = text.chars().filter(c -> c == ',').count();
To achieve in Java what you're doing in JQuery:
You first need to split the string and get an array of string with the split elements.
Then, pass the returned array to a Set in order to keep only the unique elements. Since a Set does not directly accept an array you first need to use the asList static method of the Arrays class.
Finally, either print the Set or get an array from the Set and print that.
//Splitting the string
String[] daysProxyArray = "Monday,Monday,Friday,Friday".split(",");
//Using a collection set to only keep the unique elements
Set<String> set = new HashSet(Arrays.asList(daysProxyArray));
//You could either print the set...
System.out.println(set);
//...Or returning the set's elements into an array and use that if you actually need an array
String[] res = new String[0];
res = set.toArray(res);
System.out.println(Arrays.toString(res));

PHP array to javascript loop vs json_encode [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
Hello guys i am a newbie laravel develop and in my blade i have a php var $x and this is an array!
My question what is the best method and when to apply each method?
My first metho is loop with foreach for exemple:
<script>
var javascript = [];
#foreach($x as $value)
javascript[$loop->index] = $value;
#endforeach
</script>
My second method is with #json or json_encode
<script>
var javascript = #json($x);
</script>
What method is the best?? When to aply diferent methods?
Sorry for this newbie question!!
Thanks for your help.
The most straightforward way is to just use JSON like you did with #json. This is very safe, because JSON syntax always makes for valid JavaScript expression.
It's even mentioned in the documentation, under Displaying Data.
Your first example is unnecessarily complicated and meant only for numeric-indexed arrays, so PHP's associative arrays won't work as expected.

Get child element from the json using javascript [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 4 years ago.
Improve this question
I have the below json, trying the parse the json using javscript.
var data = '{"Req":{"Header":{"tid":"1"},"Body":{"item":{"doReq":{"Header":{"id":"00"},"Body":{"test":[{"req":"000"},{"test2":"0000"}]},"temp":{"temp":"098"}}}}}}';
var json = JSON.parse(data);
console.log(json.Req.Body.item.Body);
if we use console.log(json.Req.Body.item.doReq.Body); i will get it, can we able get with out doReq.
Here i want get element without doReq, i need get direct Body. Any help.
If you need dynamic values between item and Body, you can use this:
var data = '{"Req":{"Header":{"tid":"1"},"Body":{"item":{"doReq":{"Header":{"id":"00"},"Body":{"test":[{"req":"000"},{"test2":"0000"}]},"temp":{"temp":"098"}}}}}}';
var json = JSON.parse(data);
for (var key in json.Req.Body.item) {
console.log(json.Req.Body.item[key].Body);
}

Access Object Name and convert it to String separated by comma [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 5 years ago.
Improve this question
I'm trying to access the list Object Names inside parent object and then convert them into String separated by comma. [![enter image description here][1]][1]
[1]:
In this picture, under data object, I have address, phoneFax and header objects. I can have n number of objects. I'm trying to take the names of all these objects and exclude header object.
I'm expecting the output as transaction = "address, phoneFax".
Is it possible to do it with JS?
You can use the syntax
for(var key in obj){
....
}
I created a simple JSFiddle here. Key in your case would be data, then headers, then method, etc. So you could do what I did in the JSFiddle and only loop through the data, or you could check if the key is data and loop through that key.

30 arrays in javascript is there an easy way? [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
let's say if I have 30 car models/names or just whatever....I want to put in an array such as
var names = []
is there an easy way to put all 30 into an array or I have t type it out one by one like
var names = ["subaru","toyota",]
and so on and on...
I doubt there will be a shorter way to do that. After all you have to specify all the elements and the array you want to store them in. Alternatively you may put all models in a single string separated by spaces and then split this string, but I believe the option you propose above is more readable.
If you have an input string data = "toyota subaru", then
var names = data.split(" ")
You could write a simple parser that would do that for you, and then you would simply copy/paste the whole thing into your JS file. But that might take you longer than simply typing it one by one.
If the car models/names are well structured (they are separated by a single delimiter, like a comma or space), then it might be worth it to write a simple parser. Then again it's only 30 entities...
Yes you have to type in those otherwise how the file would know what is the content of
your names array.
If you have them in an excel file, then you should use simple excel formulas to
postfix and prefix them with double quotes and qomas
then copy the forumla and paste it according to the structure so it could be like:
var myCars=new Array("subaru",
"toyota",
.
.
.
"last car name"
);

Categories