Related
Here is my problem
const data = "[\"https://i.imgur.com/PAYXC2n.jpg\",\"https://i.imgur.com/bEfjjxA.jpg\"]";
I want to convert this string to array,
expected result will be :
res =["https://i.imgur.com/PAYXC2n.jpg","https://i.imgur.com/bEfjjxA.jpg"]
I use it for mapping data in React JS project, so i need to convert this string to array.
Just parse it from JSON:
const
data = "[\"https://i.imgur.com/PAYXC2n.jpg\",\"https://i.imgur.com/bEfjjxA.jpg\"]",
parsed = JSON.parse(data);
console.log(parsed);
Do it:
const dataArr = JSON.parse(data);. This will parse as to object, which is your array (With out "")
I'm looking to convert a nested array of the type string to type float, or alternatively parsing it from a text file. Format is something along the lines of this [45.68395, 32.98629],[23.6777, 43.96555],[43.66679, 78.9648]
The first step would be to create valid JSON from your string.
If your input will always follow the schema you showed us, you could just prepend and append brackets to the string. This is not a pretty solution though. You should first check if you can get valid JSON in the first place.
A solution could look like this, provided that the input string will always follow the format of "[float, float], [float, float]":
const input = "[45.68395, 32.98629],[23.6777, 43.96555],[43.66679, 78.9648]";
// Add brackets in order to have valid JSON.
const arrayString = "[" + input + "]";
// Parse the string into an object.
const parsedArray = JSON.parse(arrayString);
// Flatten the nested array to get a one dimensional array of all values.
var flattenedArrays = [].concat.apply([], parsedArray);
// Do something with your values.
flattenedArrays.forEach(floatValue => console.log(floatValue));
You can use JSON.parse, if your numbers are actually numbers in a JSON (serialized without quotes).
let test = "[[3, 4.2], [5, 6]]";
let test2 = JSON.parse(test);
console.log(test2);
Otherwise you can simply convert your array of array of strings to array of array of numbers using + and some array mapping. :
let test = [["3", "4.2"], ["5", "6"]];
let test2 = test.map((x) => x.map((y) => +y));
console.log(test2);
Of course, you can combine both solutions if for some reason you don't control the input and have a JSON containing strings.
This thread shows you how to loop through an array of strings to convert it to an array of floats.
i hope this will work..
var input = [[45.68395, 32.98629],[23.6777, 43.96555],[43.66679, 78.9648]]
var output = [];
input.forEach(o => {
o.forEach(s => parseFloat(s))
output.push(o);
})
console.log(output);
I have the following json object:
var data = [{"name":"abc", "count":"[20.8, 100]"},
{"name":"xyz", "count":"[40, 100]"}]
Notice the array in double quotes : "[20.8, 100]"
I want the double quotes to go
Expected Output:
var data = [{"name":"abc", "count":[20.8, 100]},
{"name":"xyz", "count":[40, 100]}]
Any help would be appreciated
You could use JSON.parse and assign the value to the same property.
var data = [{"name":"abc", "count":"[20.8, 100]"}, {"name":"xyz", "count":"[40, 100]"}];
data.forEach(function (a) {
a.count = JSON.parse(a.count);
});
console.log(data);
Step 1 : Iterate the JSON using javaScript for...in loop.
Step 2 : Use JSON.parse() to convert the JSON string("[20.8, 100]") into JSON object([20.8, 100]).
Working demo :
var data = [
{"name":"abc", "count":"[20.8, 100]"},
{"name":"xyz", "count":"[40, 100]"}
];
for (var i in data) {
data[i].count = JSON.parse(data[i].count);
}
console.log(data);
I have following JSON Data:
[{"TxtDate":"\/Date(1381343400000)\/","TxtCount":7},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":2},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":1},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":3},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":0},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":7},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":10},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":0},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":177},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":13},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":6},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":10},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":9},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":52},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":0},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":1},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":0},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":0},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":0},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":0},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":2},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":7},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":45},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":0},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":0},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":0},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":27},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":1},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":0},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":14},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":0},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":9},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":2},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":84},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":0},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":6},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":0},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":16},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":2},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":4},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":21},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":6},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":0},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":13},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":18},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":10},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":1},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":5},{"TxtDate":"\/Date(1381343400000)\/","TxtCount":591}]
I want to convert it to 2-D array.
Example :
[[Date(1381343400000),7], [Date(1381343400000),2], ........]
I tried using JSON.Parse() method but it converts it to an array of objects. How do I achieve this?
You can use .map
var data = [
{"TxtDate":"\/Date(1381343400000)\/","TxtCount":7},
{"TxtDate":"\/Date(1381343400000)\/","TxtCount":2},
{"TxtDate":"\/Date(1381343400000)\/","TxtCount":1}
];
var result = data.map(function (el) {
return [
new Date(+el.TxtDate.replace(/\/Date\((\d+)\)\//gi, '$1')), // converts to JavaScript Date
el.TxtCount
]
})
console.log(result);
I have JavaScript code below.
var data = "[{"PR_ID":23096,"P_ID":23014},{"PR_ID":33232,"P_ID":23014},{"PR_ID":33308,"P_ID":23014},{"PR_ID":33309,"P_ID":23014}]";
I need convert the string to an data by delete the "" surrounding the array stored as "data" in JavaScript so after convert it suppose like below:
var data = [{"PR_ID":23096,"P_ID":23014},{"PR_ID":33232,"P_ID":23014},{"PR_ID":33308,"P_ID":23014},{"PR_ID":33309,"P_ID":23014}];
How to make the convert?
To convert a JSON object to Javascript object use:
var data = '[{"PR_ID":23096,"P_ID":23014},{"PR_ID":33232,"P_ID":23014},{"PR_ID":33308,"P_ID":23014},{"PR_ID":33309,"P_ID":23014}]';
JSON.parse(data);
But first change the double quote to single quote, otherwise the JSON object wont be a valid JSON.
After this you can walk the array in the following way:
var jsonParsed = JSON.parse(data);
for(var val in jsonParsed) {
if(jsonParsed.hasOwnProperty(val)) {
// do something with the values
}
}