Convert JSON Data to JavaScript 2D Array - javascript
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);
Related
Convert array string to array for mapping data
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 "")
Converting json from json_ecode() to a javascript array
I am using the json_encode() in php to return a json object. How can i convert it to a javascript array? input: [{"message":"1"},{"message":"2"}] output ["1", "2"]
this way const data = '[{"message":"1"},{"message":"2"}]' const result = JSON.parse(data).map(o=>o.message) console.log( result )
Parsing data with unknown JSON nodes
I am trying to convert some JSON into a javascript arrays, however, the date is used a key as well as an attribute in the node. I wanted to use a map function to extract the dates, however, I have the extra date key which I don't know ahead of time. Do I have to loop through the keys and parse out the data or is there a clever way I can use javascript functions like map to do it? var label_Dates = data.map(({date}) => date); var label_Data = data.map(({totalAssets}) => totalAssets); JSON data: 2020-03-31: {...} date: "2020-03-31" totalAssets: "300280000000.00" 2019-12-31: {...} date: "2019-12-31" totalAssets: "306928000000.00" 2019-09-30: {...} 2019-06-30: {...} 2019-03-31: {...} 2018-12-31: {...} 2018-09-30: {...} 2018-06-30: {...} 2018-03-31: {...}
This example shows you have to put all of the values of a JSON object into an array: var json = {"date1":"aa","date2":"cc"}; const arr = Object.values(json); console.log(arr); Output: ['aa', 'cc']
Can you pass an array of index numbers to the array push method instead of specifying each index in push specifically
I want to create a new 2D array from another 2D array retaining specific columns from the original array that are defined in a variable. I have a working version that uses hardcoded values of which columns from the original array to retain but I need a version that uses a variable. var data = [ [1,5,7,8,9,98,56], [4,7,8,9,2,55,88], [3,3,4,3,3,24,11] ]; var indexes2Keep = [0,2,6]; data.forEach(function(row) { slicedData.push( [ row[2], row[4], row[6] ] ); }); Instead of having the columns hardcoded in the array push method how can I use the values of the variable indexes2Keep to give the same result. thanks Expected output is: slicedData = [ [1,7,56], [4,8,88], [3,4,11] ];
You can use Array.map/Array.filter: var data = [ [1,5,7,8,9,98,56], [4,7,8,9,2,55,88], [3,3,4,3,3,24,11] ]; var indexes2Keep = [0,2,6]; var slicedData = data.map(function (row){ return row.filter(function(_,i){ return indexes2Keep.indexOf(i) !== -1 }) }) //Alternatively var slicedData2 = data.map(function (row){ return indexes2Keep.map(function(i){ return row[i] }) }) console.log(slicedData) console.log(slicedData2)
Simply call .map on that array to map each index to the element at that index in the row. data.forEach(function(row) { slicedData.push(indexes2Keep.map(function(index) {return row[index]})); });
You could use the map() function for this: slicedData.push(indexes2Keep.map(index => row[index]));
How to convert a string version of a number array in json to a number array in javascript
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);