Convert array string to array for mapping data - javascript

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 "")

Related

javascript - How to split JSON array into separate arrays for DataTables

I am trying to take a JSON array like this
[Alex, James, John]
and create seperate JSON arrays for them like this
[[Alex], [James], [John]]
So I can then insert the JSON payload into a datatable using Datatables.JS
So far I have this
var data = json;
while (data.length) {
console.log(data.splice(0, 1));
}
return data;
Now this splits the JSON into 3 seperate arrays but when I try to use the data variable in the datatables.JS, it complains that is not in the proper format because it is
[Alex]
[James]
[John]
in the console.
I want to combine them into the JSON payload described above so I can insert it into the datatable.
I think using the map method helps in your case!
If this is your JSON data = "["Alex","James","John"]". You can parse it with JSON.parse("["Alex","James","John"]"), this would results you an array object as ['Alex', 'James', 'John']
let personArray = ['Alex', 'James', 'John'];
let resultantArray = personArray.map(name => [name]);
console.log(resultantArray);
Ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
I would do this:
let ar = ["Alex", "James", "John"]
let ar1 = ar.slice(1)
let ar2 = ar.splice(0, 1)
console.log([ar2, ar1])
This solution assumes that you have already parsed your json into a Javascript array

A nested array of string to number

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);

Fetch localstorage value in array

I am saving value in localstorage as shown below
key = profskill , value = "a,b,c"
In my test.ts file, I have declared array but I am unable to fetch the result in it. Code shown below:
getskills: Array<string> = [];
this.getskills = localStorage.getItem("profskill");
but this is giving error:
Type 'string' is not assignable to type 'string[]'
I want to fetch value like this:
console.log(this.getskills[0]);
The LocalStorage can only store strings, not objects or arrays. If you try to store an array, it will automatically be converted to a string. You need to parse it back to an array :
JSON.parse( localStorage.getItem("profskill") )
Since, you want the comma separated value to be represented as a array of strings for this.getskills use split on the value of the localStorage
Here is a sample example
//say we get the value 'a,b,c' from localStorage into the temp variable
//var temp = localStorage.getItem(profskill);
var temp= 'a,b,c';
this.getskills = temp.split(',');
console.log(this.getskills[0]);
localStorage only supports strings. Use JSON.stringify() to set the data in storage and JSON.parse() to get the data from storage and then use split(",") to split the comma separated data.
var obj = "a,b,c";
localStorage.setItem("profskill", JSON.stringify(obj));
var getskills = [];
getskills = JSON.parse(localStorage.getItem("profskill")).split(",");
console.log(getskills[0]);
First get the data from the LocalStorage:
var DataTableValue = JSON.parse(localStorage.getItem('dataTableValue'));
Then, store in an Array:
var tempArray = new Array();
for (var i = 0; i < DTarray.length; i++) {
tempArray.push(DTarray[i]);
}
All data will be stored in the variable tempArray.

Convert JSON Data to JavaScript 2D Array

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);

javascript convert string to data

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
}
}

Categories

Resources