I had posted this question earlier but someone deleted with How can I access and process nested objects, arrays or JSON?
as possible answer. This is not helping me since a) The key 'date' is spread across several names, b)The objects comprises on arrays & objects & c) The depth at which the key 'date' is present can change.
Hence posting the question again.
I have a JS object as below
const bb = {
Harry: {
details: [
{
cat: "Life",
values: [
{
date: "2021-08-02",
level: 77.6,
},
{
date: "2021-08-03",
level: 79.1,
},
],
},
],
},
Barry: {
details: [
{
cat: "Logic",
values: [
{
date: "2021-08-02",
level: 77.6,
},
{
date: "2021-08-03",
level: 79.1,
},
],
},
],
},
};
I also have a variable defined for parsing the dates const dateParse = d3.timeParse("%Y-%m-%d")
I want to parse all the dates in the object. Since the 'date' is few levels below in the object, I am not able to figure this out. How do I go about it?
The expected output is
const bb = {
Harry: {
details: [
{
cat: "Life",
values: [
{
date: Mon Aug 02 2021 00:00:00 GMT+0530 (India Standard Time),
level: 77.6,
},
{
date: Tue Aug 03 2021 00:00:00 GMT+0530 (India Standard Time),
level: 79.1,
},
],
},
],
},
Barry: {
details: [
{
cat: "Logic",
values: [
{
date: Mon Aug 02 2021 00:00:00 GMT+0530 (India Standard Time),
level: 77.6,
},
{
date: Tue Aug 03 2021 00:00:00 GMT+0530 (India Standard Time),
level: 79.1,
},
],
},
],
},
};
You just have to loop through the objects, select the date node and execute
(new Date(datestring)).toString()
This will generate the date as specified in your output.
const bb = { Harry: { details: [{cat: "Life",values: [{date: "2021-08-02",level: 77.6},{date: "2021-08-03",level: 79.1}]}]},Barry: {details: [{cat: "Logic",values: [{date: "2021-08-02",level: 77.6},{date: "2021-08-03",level: 79.1}]}]}};
Object.values(bb).forEach((value) => {
value.details.forEach((detail) => {
detail.values.forEach((value) => {
value.date = (new Date(value.date)).toString();
})
})
});
console.log(bb);
If you want to parse all the nested date keys, you can do it recursively using the below functions
const bb = { Harry: { details: [{cat: "Life",values: [{date: "2021-08-02",level: 77.6},{date: "2021-08-03",level: 79.1}]}]},Barry: {details: [{cat: "Logic",values: [{date: "2021-08-02",level: 77.6},{date: "2021-08-03",level: 79.1}]}]}};
function traverseArray(inputArray) {
for (const arrayValue of inputArray) {
traverseObject(arrayValue);
}
}
function traverseObject(inputObject) {
for (const key in inputObject) {
const value = inputObject[key];
const valueType = typeof(value);
if (valueType == "object") {
traverseObject(value);
} else if (valueType == "array") {
traverseArray(value);
} else if (key == "date") { // since I want to parse only date keys
inputObject[key] = 'd3.timeParse("%Y-%m-%d") - Parse date here';
}
}
}
traverseObject(bb);
console.log(bb);
Related
I'm referring to this answer again.
var firstEvents = events.reduce(function(ar, e) {
var id = e.getId();
if (e.isRecurringEvent() && e.isAllDayEvent() && !ar.some(function(f) {return f.eventId == id})) {
ar.push({eventTitle: e.getTitle(), eventId: id, startDate: e.getAllDayStartDate(), endDate: e.getAllDayEndDate()});
}
return ar;
}, []);
What do I have to change to get an array with the event titles (Strings) as keys and the start dates (Date objects) as values so I can retrieve a certain start date (Date object) via firstEvents['some event title']?
EDIT:
Current Ouput:
firstEvents = [{eventTitle=Event title 1, eventId=xyz1#google.com, startDate=Sun Mar 18 00:00:00 GMT+01:00 2018, endDate=Mon Mar 19 00:00:00 GMT+01:00 2018},
{eventTitle=Event title 2, eventId=xyz2#google.com, startDate=Tue Mar 19 00:00:00 GMT+01:00 2019, endDate=Wed Mar 20 00:00:00 GMT+01:00 2019},
{eventTitle=Event title 3, eventId=xyz3#google.com, startDate=Fri Mar 20 00:00:00 GMT+01:00 2020, endDate=Sat Mar 21 00:00:00 GMT+01:00 2020}]
Needed Output (Pseudo):
firstEvents = ['Event title 1' => Sun Mar 18 00:00:00 GMT+01:00 2018,
'Event title 2' => Tue Mar 19 00:00:00 GMT+01:00 2019,
'Event title 3' => Fri Mar 20 00:00:00 GMT+01:00 2020]
Do not use push, but set to object with key.
ar = {}; // You may need to change source parameter too
// you cannot change input Array [] to Object {} type inside function
// you can get Array and return Object, but source variable will not change
ar[e.getTitle()] = e.getAllDayStartDate();
Or using some demo data:
var ar = [
{
eventTitle: 'one',
eventId: '#1',
startDate: new Date(),
endDate: new Date()
},
{
eventTitle: 'two',
eventId: 'secondId',
startDate: new Date(),
endDate: new Date()
}];
var retVal = {};
for (var i of ar) {
retVal[i.eventId] = i;
}
console.log(JSON.stringify(retVal, null, 2));
console.log(retVal['#1']);
console.log(retVal.secondId);
This is my Array
$scope.tooltipsArray = [
{
date: 2018-10-10T07:03:43.835Z,
text: 'name1'
},
{
date: 2018-09-29T18:30:00.000Z,
text: 'name2'
}
];
How can I update date to locale date format like this.
$scope.tooltipsArray = [
{
date: Wed Oct 10 2018 14:05:27 GMT+0530 (India Standard Time),
text: 'name1'
},
{
date: Sun Sep 30 2018 00:00:00 GMT+0530 (India Standard Time),
text: 'name2'
}
];
I have used map() to do that. But it does not work
var vector = $scope.tooltipsArray.map(function (el) { return new Date(el.date).toLocaleDateString(); });
Can anyone tell me how to do this from map() in JavaScript?
You can use the below code -
$scope.tooltipsArray = [
{
date: "2018-10-10T07:03:43.835Z",
text: 'name1'
},
{
date: "2018-09-29T18:30:00.000Z",
text: 'name2'
}
];
var vector = $scope.tooltipsArray.map(function(el) {return { 'date':new Date(el.date).toString(),'text':el.text}});
console.log(vector);
The output will be like below -
[
{date: "Wed Oct 10 2018 12:33:43 GMT+0530 (India Standard Time)", text: "name1"}
{date: "Sun Sep 30 2018 00:00:00 GMT+0530 (India Standard Time)", text: "name2"}
]
Why is there a .value key after tooltipsArray?
You assigned the array to tooltipsArray, so unless there's a Proxy involved, expect to access the array through $scope.tooltipsArray.
To fix it, just remove .value.
var vector = $scope.tooltipsArray.map(function (el) { return new Date(el.date).toLocaleDateString(); });
1- Remove .value why is there in the first place?
2- You need to change the date inside the object and then return el instead of date if you just want the date to be changed, likewise:
var vector = $scope.tooltipsArray.map(function(el) {
el.date = new Date(el.date).toLocaleDateString();
return el;
});
What map function does is going through array element one by one and run the callback function, so what you have to do is update the whole object or update one entry
el.date = new Date(el.date).toLocaleDateString();
This is my array
someArray = [
{
city: colombo1,
text: 'name1',
newArray[ date: 2018-09-29T18:30:00.000Z, discription: 'none']
}
{
city: colombo2,
text: 'name',
newArray[ date: 2018-10-10T07:03:43.835Z, discription: 'none']
}
];
I want to update my array as below
someArray = [
{
city: colombo1,
text: 'name1',
newArray[ date: Sun Sep 30 2018 00:00:00 GMT+0530 (India Standard Time), discription: 'none']
}
{
city: colombo2,
text: 'name',
newArray[ date: Wed Oct 10 2018 14:05:27 GMT+0530 (India Standard Time), discription: 'none']
}
];
I try to use map() to update my array. But I cannot understand the logic to do that. Can anyone please help me. Thank you
i hope newArray is an array which has 'date' as index,
for(var i=0;i<someArray.length;i++){
someArray[i].newArray['date'] = new Date(someArray[i].newArray['date']).toString();
}
If you want use map try something like this:
var mappedArray = someArray.map(function(item){
item.newArray['date'] = convertDate(item.newArray['date']);
});
https://jsfiddle.net/83sqfxc0/1/
I am trying to figure out how to manipulate my JSON data and create a separated object with the manipulated data.
To be more precise, I am using this JSON file: https://jsonblob.com/57a70ca4e4b0dc55a4eb1f73
I am trying to convert [dataset][data] into this format:
[Date.UTC(2013,5,2),0.7695],
[Date.UTC(2013,5,3),0.7648],
[Date.UTC(2013,5,4),0.7645],
[Date.UTC(2013,5,5),0.7638],
[Date.UTC(2013,5,6),0.7549]
So I need to use the fields [dataset][data][i][0] (Date) and [dataset][data][i][4] (Value) and somehow put it in a new JSON formatted object but I have no idea how to do it.
Someone can help me with this situation?
$(function () {
$.getJSON('json/AAPL.json', function (data) {
// Data manipulation into a new object??
});
});
This should do it:
$(function () {
$.getJSON('json/AAPL.json', function (data) {
var correctFormat = data.dataset.data.map(function(item) {
return [new Date(item[0]), item[4]];
});
});
});
Your problem is just map one :
You have an array of elements (arrays themselves) and you want to keep and modify just first and fifth element of each sub-array
So you can do it like this
var data = [
[
"2016-08-02",
106.05,
106.07,
104,
104.48,
32731069,
0,
1,
106.05,
106.07,
104,
104.48,
32731069
],
[
"2016-08-01",
104.41,
106.15,
104.41,
106.05,
37141424,
0,
1,
104.41,
106.15,
104.41,
106.05,
37141424
],
[
"2016-07-29",
104.19,
104.55,
103.68,
104.19,
27076805,
0,
1,
104.19,
104.55,
103.68,
104.19,
27076805
]
];
var result = data.map(x => [Date(x[0]), x[4]]);
console.log(result); // [ [ 'Sun Aug 07 2016 14:29:22 GMT+0200 (Paris, Madrid (heure d’été))',104.48 ],
// [ 'Sun Aug 07 2016 14:29:22 GMT+0200 (Paris, Madrid (heure d’été))',106.05 ],
// [ 'Sun Aug 07 2016 14:29:22 GMT+0200 (Paris, Madrid (heure d’été))',104.19 ] ]
I'm attempting to use underscore to filter out certain properties of an object. The beginning of the following code works as expected, but .pick is not working. I'm aiming to limit the properties of the returned object to those strings listed in the .pick method.
var result = _.chain(data)
.each(function(item) {
item.answers = [];
_.each(data, function(object) {
if (item.id === object.id) {
item.answers.push({
id: object.answer_id,
email: object.answer_email,
date: object.answer_date
});
}
});
item = _.pick(item,
'id',
'owner_id',
'url',
'enabled',
'review_date',
'answers'
);
})
.uniq(function(item) {
return item.id;
})
.value();
The array I start with, 'data', looks like this:
[
{
id: '8ffdf27b-5a90-478a-b263-dhhdhdhhdhd',
answer_date: Fri Oct 30 2015 14:35:07 GMT-0400 (EDT),
answer_id: 1,
answer_email: 'test#example.com',
owner_id: 5,
url: 'media/5-4a3640ac-ec13-fhhfh-ac0a-fhjhdhhdhd.jpg',
enabled: false,
review_date: Sun Nov 01 2015 13:57:32 GMT-0500 (EST)
}, ...
]
The returned array 'should' look like so:
[
{
id: '8ffdf27b-5a90-478a-b263-dhhdhdhhdhd',
owner_id: 5,
url: 'media/5-4a3640ac-ec13-fhhfh-ac0a-fhjhdhhdhd.jpg',
enabled: false,
review_date: Sun Nov 01 2015 13:57:32 GMT-0500 (EST),
answers: [{...}, {...}]
}, ...
]
but instead currently looks like this:
[
{
id: '8ffdf27b-5a90-478a-b263-dhhdhdhhdhd',
answer_date: Fri Oct 30 2015 14:35:07 GMT-0400 (EDT),
answer_id: 1,
answer_email: 'test#example.com',
owner_id: 5,
url: 'media/5-4a3640ac-ec13-fhhfh-ac0a-fhjhdhhdhd.jpg',
enabled: false,
review_date: Sun Nov 01 2015 13:57:32 GMT-0500 (EST),
answers: [{...}, {...}]
}, ...
]
You should use map() instead of each() to change your array (note that you have to return the modified item in the map function):
var result = _.chain(data)
.map(function (item) {
item.answers = [];
_.each(data, function (object) {
if (item.id === object.id) {
item.answers.push({
id: object.answer_id,
email: object.answer_email,
date: object.answer_date
});
}
});
item = _.pick(item,
'id',
'owner_id',
'url',
'enabled',
'review_date',
'answers'
);
return item;
})
.uniq(function (item) {
return item.id;
})
.value();