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']
Related
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
I have two arrays created from a reduce method that searches two existing arrays for dates. It then turns those dates into unique object keys.
The first group is a list of names the second is a set of series of instructions.
The goal is to combine the two arrays that get formed by finding like object keys and adding the matching list object and insert it into the set of instructions with the object key list.
For one it makes an array that looks like :
const listObj = [
{11/20/2020: [{name:'Joe', date:11/20/2020}]},
{11/26/2020 : [{name:'John', date:11/26/2020}]}
]
And this:
const scheduleObj = [
{11/20/2020: {type:'Federal', date:11/20/2020}},
{11/26/2020 : {type:'State', date:11/26/2020}}
]
The final product that i need would look something like:
const scheduleObj = [
{11/26/2020 : {
type: 'State',
list: [{name:'John', date:11/26/2020}]
},
...
]
Where the list is an added key and the array is the array that is associated with the object key
I have used a messy what looks like lodash method to get this to work, but I figure there has to be some sort of mapping I can do.
Any Help Would Be Appreciated
This can be little messy and not failure proof depending on what you have in your listObj or scheduleObj. E.g. repeated date in scheduleObj can lead to a problem using this method, you may have to use another key if the dates are no unique in both lists.
scheduleObj.map((s) => {
// get the first key of the object as the date you're going to group with
const date = Object.keys(s)[0];
// filter the object that matches the date
const listObjFilter = listObj.filter((o) => Object.keys(o)[0] === date);
// get the list for the given date (or empty array if nothing found)
const listForDate = listObjFilter.length ? listObjFilter[0][date] : [];
// build the result object
return {
[date]: {
type: s[date]['type'],
list: listForDate
}
};
})
Note that I'm always considering you have only one key in the objects inside the lists in listObj or scheduleObj.
i have this array obj:
var scenari =[
{pos:'i4y',azione:('persa','avanti','indietro'),'peso':(0.3,0.4,0.3)},
{pos:'g4r',azione:('persa','avanti','indietro'),'peso':(0.3,0.4,0.3)}
];
How to retrieve the array in key azione?
i try this but is print only 'indietro' and not array
console.log (scenari[0]['azione']);//indietro
Parentheses not define an array and must be use brackets ([]):
var scenari =[
{pos:'i4y',azione:['persa','avanti','indietro'],'peso':[0.3,0.4,0.3]},
{pos:'g4r',azione:['persa','avanti','indietro'],'peso':[0.3,0.4,0.3]}
];
console.log (scenari[0]['azione']);//indietro
You are using () instead of [].
If you use () last value will be the value of key
var scenari =[
{pos:'i4y',azione:['persa','avanti','indietro'],'peso':(0.3,0.4,0.3)},
{pos:'g4r',azione:['persa','avanti','indietro'],'peso':(0.3,0.4,0.3)}
];
console.log (scenari[0]['azione']);
//If you use ()
//Example:
var ke = ('d','e');
console.log(ke);
You are getting this issue because tuple javascript is treating the data in () as a expression, so to get the result in the way you want you have to use [] or convert your data in string .
I am getting the response from ajax as json:
{"checkin_date":["2017-05-11","2017-05-18","2017-05-23","2017-05-25"]}
I need to separate these values in javascript like 2017-05-11, 2017-05-18, etc.
I tried split function in javascript but it failed. How do I achieve it?
I need the dates separately in a loop.
Parse the JSON string using JSON.parse method and get the property which holds the array.
var dates = JSON.parse(respose_data).checkin_date
// or
var dates = JSON.parse(respose_data)['checkin_date']
If it's an object then directly access the property.
var dates = respose_data.checkin_date
// or
var dates = respose_data['checkin_date']
UPDATE : And now iterate over the array using Array#forEach or a simple for loop.
dates.forEach(function(v){
console.log(v);
})
// or
for(var i = 0;dates.length < i; i++){
console.log(dates[i]);
})
You just use method parse of JSON. After that, you receive array value for property "checkin_date". Just use it as normal array. See code below:
var json = '{"checkin_date":["2017-05-11","2017-05-18","2017-05-23","2017-05-25"]}';
var result = JSON.parse(json);
var checkin_date = result['checkin_date'];
for(var i=0; i<checkin_date.length; i++){
console.log(checkin_date[i]);
}
You can do a simple solution using arrow function:
const json = {"checkin_date":["2017-05-11","2017-05-18","2017-05-23","2017-05-25"]}
json.checkin_date.map((date) => date)
If you need it on pre-ES6 JavaScript, just do it like:
var json = {"checkin_date":["2017-05-11","2017-05-18","2017-05-23","2017-05-25"]}
json.checkin_date.map(function(date) { return date })
Both ways you will have returned all the dates from the loop.
You could:
Parse content of your request to a JavaScript object using JSON.parse().
Take property checkin_date which contains an Array with the list of dates.
Loop the Array and do whatever you need which each date.
let respose_data = '{"checkin_date":["2017-05-11","2017-05-18","2017-05-23","2017-05-25"]}';
let dates = JSON.parse(respose_data).checkin_date;
dates.forEach(date=> console.log(date))
More info on JSON.parse()
If you need to loop through each value as a date, you first need to convert the values. Take a look:
var dates = {"checkin_date":["2017-05-11","2017-05-18","2017-05-23","2017-05-25"]};
dates
.checkin_date
.map(date => new Date(date)) // here you convert the string dates to values of type Date
.forEach(date => console.log(date)) // here you loop through every Date and do whatever you want, in this case I just logged the output.
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);