How do i convert this string into an array? [duplicate] - javascript

This question already has answers here:
Parse JSON in JavaScript? [duplicate]
(16 answers)
Safely turning a JSON string into an object
(28 answers)
Closed 3 years ago.
I need to convert this string into a an array into the format shown below.
itemString
[{"_id":"1e12345","a":"abc","b":def","c":ghi"},{"_id":"1e678910","a":"xzx","b":sed","c":ert"}]
var item=itemString.split("},{").map(function(i){
return i
})
console.log(item)
what i need to get from from console.log(item)
0:{_id:"1e12345",a:"abc",b:"def",c:"ghi"}
1:{_id:"1e678910",a:"xzx",b:"sed",c:"ert"}
what i get from console.log(item)
0:"[{"_id":"1e12345","a":"abc","b":"def","c":"ghi""
1:""_id":"1e678910","a":"xzx","b":"sed","c":"ert"}]"

Related

how to make object in array to be array [duplicate]

This question already has answers here:
Parsing string as JSON with single quotes?
(10 answers)
Parsing string as JSON with single quotes on key and keyvalue [duplicate]
(2 answers)
Converting array inside a string to an array
(2 answers)
Parsing "relaxed" JSON without eval
(6 answers)
Closed 1 year ago.
let origin = "[{'a':1, 'b':2}, {'a':3, 'b':4}, {'a':5, 'b':6}]"
let output = [{'a':1, 'b':2}, {'a':3, 'b':4}, {'a':5, 'b':6}]
How to make origin to be output as a array

How to put variable into quotes? [duplicate]

This question already has answers here:
Double quote in JavaScript string
(4 answers)
How to interpolate variables in strings in JavaScript, without concatenation?
(17 answers)
How to insert variables in JavaScript strings?
(4 answers)
Closed 3 years ago.
Would like to put the variable ( txt.txt.charAt(char_counter) )in quotes.
Anyone can help?
Thanks
var pattern_node = document.createTextNode("Zeichen:"+txt.txt.charAt(char_counter)+" "+ str.str +" gesendet");
ES6/ES2015 feature, template string
var pattern_node = document.createTextNode(`Zeichen:${txt.txt.charAt(char_counter)} ${str.str} gesendet`);

Convert raw string to array [duplicate]

This question already has answers here:
How to convert an array like string to array in node.js?
(4 answers)
Closed 3 years ago.
I need to convert raw String to json array in javascript below the my logic
Original:
"[template1,template2]";
Exception:
"["template1","template2"]";
use slice to retrieve the text between the square brackets and then use split.
const input = "[template1,template2]";
const arr = input.slice(1, -1).split(',');
console.log(arr);

How to format string in JS? [duplicate]

This question already has answers here:
JavaScript .replace only replaces first Match [duplicate]
(7 answers)
Closed 6 years ago.
I would like to have the result :
28,12,2016
From this string "28/12/2016"
I tried :
("28/12/2016").replace('/',',');
==>"28,12/2016"
I don't know how to delete the second /and the " "
use split and join method
var a="28/12/2016";
var ans=a.split("/").join(",");
console.log(ans);

convert string to map in javascript [duplicate]

This question already has answers here:
Parse JSON in JavaScript? [duplicate]
(16 answers)
Closed 7 years ago.
how to convert the following string to map so that i can access the values
data.message ="{"id":"60653","key":"project1","self":"http://127.0.0.1:321/rest/api/2/issue/project1"}"
if your string is a valid JSON just use the JSON.parse function:
var str = '{"id":"60653","key":"project1","self":"http://127.0.0.1:321/rest/api/2/issue/project1"}'
var mydata = JSON.parse(str);
Use JSON.parse
Here's the corresponding MDN documentation:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse

Categories

Resources