react native convert string into array data - string

i have string like this:
'["wss://mediaslave1-dev-v2.vedax.ai/1L1g5nSOrNBr1fBWBva7","https://mediaslave1-dev-v2.vedax.ai/PMKzhcRhIEugeqowXMpc"]'
I want to convert into array, can someone help me

const str ='["wss://mediaslave1-dev-v2.vedax.ai/1L1g5nSOrNBr1fBWBva7","https://mediaslave1-dev-v2.vedax.ai/PMKzhcRhIEugeqowXMpc"]'
const arr = JSON.parse(str);
console.log(arr);
You may use JSON.parse

Related

Convert Specific String to JSON Object

const test ="[{contactId=2525, additionDetail=samle}]";
I need to convert this string to a JSON object. It will dynamically load like this string. I need to particular string to convert to a JSON object.
JSON.parse(test) command not working for this. I attached the error here.
For that specific string, you'd have to parse it yourself.
const test = '[{contactId=2525, additionDetail=samle}]';
const obj = {};
test.split(/[{}]/)[1].split(/, /).forEach((elm) => {
const entry = elm.split('=');
obj[entry[0]] = entry[1];
});
What I am doing is splitting the string on the braces and selecting the second element (utilising regex) then splitting that on comma and space (again regex) then loop over the result and assign to an object.
You can then JSON.stringify(obj) for the result.
:edit:
For the second string you've asked for there is another, potentially more refined, answer. You'll need to first replace the = with : (I've again used a regex), then you use a regex to match the words and sentence and use a function to add the quotes.
const test = '[{contactId=2525, additionDetail=samle}]';
const test2 = "[{contactId=2525, additionDetail=rrr additional Detail, medicationType={medicationTypeId=3333, medicationType=Tablet}, endDate=2022-12-30}]";
const replaced = test.replace(/=/g,':')
const replaced2 = test2.replace(/=/g, ':');
const replacer = function(match){
return '"' + match + '"';
}
const replacedQuote = replaced.replace(/(?!\s)[-?\w ?]+/g,replacer);
const replaced2Quote = replaced2.replace(/(?!\s)[-?\w ?]+/g,replacer);
const obj = JSON.parse(replacedQuote);
const obj2 = JSON.parse(replaced2Quote);
You should note that Json means javascript object notation, so you need to create a JavaScript object to get started:
const test ="[{contactId=2525, additionDetail=samle}]";
let obj = Object.create(null)
You can now define your variable as one of the object properties :
obj.test = test
Now we have a JavaScript object and we can convert it to json:
let convertedToJson = JSON.stringify(test);
[{contactId=2525, additionDetail=samle}]
this is not a valid JSON-string, and it cannot be parsed by JSON.parse()
the correct JSON-string would be:
const test ='[{"contactId":2525, "additionDetail":"samle"}]';

How to convert an array of list to a single list in node js

I have an array in the format
[{"Name":"abc","mark":[10,20,30]},{"Name":"def","mark":[10,20,30]}]
is there a way to convert this array in the following format in node js i tried iterating over and pushing but still not able to get it in following format,
{"abc":[10,20,30],"def":[10,20,30]}
Can any one help me in this?
You could use a simple forEach to loop inside the array and create result object like:
const input = [{"Name":"abc","mark":[10,20,30]},{"Name":"def","mark":[10,20,30]}];
let result = {};
input.forEach(x => {
result[x.Name] = x.mark;
})
console.log(result)
You can use reduce to get the desired output
const input = [{"Name":"abc","mark":[10,20,30]},{"Name":"def","mark":[10,20,30]}];
const output = input.reduce((acc, curr) => { return {...acc, [curr.Name]: curr.mark}}, {})
Documentation

Distinct like String Manipulation

I working on Flutter project. I have query regarding string manipulation. I storing some value like following
String a="31,31,31,31,31,31,31,41,41,41,41,41,41,41,41,41,41,53,53,53,53,53,53,53,53"
I have output like Distinct in query
a="31,41,53"
may I know how to achieve this function.
Thanks in advance
Sathish
void main() {
String a =
"31,31,31,31,31,31,31,41,41,41,41,41,41,41,41,41,41,53,53,53,53,53,53,53,53";
var values = a.split(',');
var result = Set.from(values).join(',');
print(result);
}
Result:
31,41,53

Convert the object into a string and bring in the necessary form

An object comes to me. I write the variable key and the value through the loop:
let update_info = [];
for (let[key, value] of Object.entries(req.body)) {
update_info.push(`${key} = ${value}`);
}
console.log(JSON.parse(update_info));
Output to console:
undefined:1
user_name = name,user_email = #email.com,user_password = 12345678,about = aboutaboutaboutabout
^
SyntaxError: Unexpected token u in JSON at position 0
Why it happens?
I need to be displayed in the console like this:
'user_name' = 'name','user_email' = '#email.com','user_password' = '12345678','about' = 'aboutaboutaboutabout
How do i implement this?
I've reproduced your code like this and all you need to do is
JSON.stringify turns a JavaScript object into JSON text and stores that JSON text in a string.
JSON.parse turns a string of JSON text into a JavaScript object.
let obj = {
"welcome": "hello",
"reply": "hi"
}
let update_info = [];
for (let[key, value] of Object.entries(obj)) {
update_info.push(`${key} = ${value}`);
}
console.log(JSON.stringify(update_info));
Try the below code:
You need not to parse it using JSON.parse because the array is not yet stringified, so you should use toString() to achieve the desired result
let update_info = [];
for (let[key, value] of Object.entries(req.body)) {
update_info.push(`'${key}' = '${value}'`);
}
console.log(update_info.toString());
If you are interested in printing the Object key value pair in the console to have better view use
console.log(JSON.stringify(object, undefined, 2))
This will print the object in proper format indented by 2 spaces

Array of json inside string to Array of json nodejs

I am having string in two format as
[{"a":"a1"},{"a":"a2"}]'
I actually extract it in array:
[{"a":"a1"},{"a":"a2"}]
How to convert it?
Use JSON.parse()
const string = '[{"a":"a1"},{"a":"a2"}]'
const res = JSON.parse(string)
console.log('Result : ', res)
here is your string as arrayString:
const arrayString = `[{"a":"a1"},{"a":"a2"}]`;
JSON.parse(arrayString); // here you will find your desired result.
you can use JSON.parse to parse data JSON that is in string format

Resources