I've been trying to sort my dates in chronological order but it doesn't seem to be working.
var data = JSON.parse(body);
_.sortBy(data,function(a){
return a.results;
});
Yet the output I'm getting is still wrong. The last few entries are not in the right place. The data being read is like this.
{"count": 56, "results": [{"date": "2014-06-05T18:02:51", "id": "LC8_L1T_TOA/LC80370352014156LGN01"}, {"date": "2014-06-21T18:02:55", "id": "LC8_L1T_TOA/LC80370352014172LGN00"}, {"date": "2014-07-07T18:03:03", "id": "LC8_L1T_TOA/LC80370352014188LGN00"}, {"date": "2014-07-23T18:03:05", "id": "LC8_L1T_TOA/LC80370352014204LGN00"}, {"date": "2014-08-08T18:03:14", "id": "LC8_L1T_TOA/LC80370352014220LGN00"}, {"date": "2014-08-24T18:03:17", "id": "LC8_L1T_TOA/LC80370352014236LGN00"}, {"date": "2014-09-09T18:03:21", "id": "LC8_L1T_TOA/LC80370352014252LGN00"}, {"date": "2014-09-25T18:03:18", "id": "LC8_L1T_TOA/LC80370352014268LGN00"}, {"date": "2014-10-11T18:03:25", "id": "LC8_L1T_TOA/LC80370352014284LGN00"}, {"date": "2014-10-27T18:03:23", "id": "LC8_L1T_TOA/LC80370352014300LGN00"}, {"date": "2014-11-12T18:03:26", "id": "LC8_L1T_TOA/LC80370352014316LGN00"}, {"date": "2014-11-28T18:03:24", "id": "LC8_L1T_TOA/LC80370352014332LGN00"}, {"date": "2014-12-14T18:03:18", "id": "LC8_L1T_TOA/LC80370352014348LGN00"}, {"date": "2014-12-30T18:03:14", "id": "LC8_L1T_TOA/LC80370352014364LGN00"}, {"date": "2015-01-15T18:03:14", "id": "LC8_L1T_TOA/LC80370352015015LGN00"}, {"date": "2015-01-31T18:03:10", "id": "LC8_L1T_TOA/LC80370352015031LGN00"}, {"date": "2015-02-16T18:03:01", "id": "LC8_L1T_TOA/LC80370352015047LGN00"}, {"date": "2015-03-04T18:02:56", "id": "LC8_L1T_TOA/LC80370352015063LGN00"}, {"date": "2015-03-20T18:02:46", "id": "LC8_L1T_TOA/LC80370352015079LGN00"}, {"date": "2015-04-05T18:02:35", "id": "LC8_L1T_TOA/LC80370352015095LGN00"}, {"date": "2015-04-21T18:02:34", "id": "LC8_L1T_TOA/LC80370352015111LGN00"}, {"date": "2015-05-07T18:02:18", "id": "LC8_L1T_TOA/LC80370352015127LGN00"}, {"date": "2015-05-23T18:02:16", "id": "LC8_L1T_TOA/LC80370352015143LGN00"}, {"date": "2015-06-08T18:02:26", "id": "LC8_L1T_TOA/LC80370352015159LGN00"}, {"date": "2015-06-24T18:02:32", "id": "LC8_L1T_TOA/LC80370352015175LGN00"}, {"date": "2015-07-10T18:02:43", "id": "LC8_L1T_TOA/LC80370352015191LGN00"}, {"date": "2015-07-26T18:02:49", "id": "LC8_L1T_TOA/LC80370352015207LGN00"}, {"date": "2015-08-11T18:02:53", "id": "LC8_L1T_TOA/LC80370352015223LGN00"}, {"date": "2015-08-27T18:03:00", "id": "LC8_L1T_TOA/LC80370352015239LGN00"}, {"date": "2015-09-12T18:03:07", "id": "LC8_L1T_TOA/LC80370352015255LGN00"}, {"date": "2015-09-28T18:03:13", "id": "LC8_L1T_TOA/LC80370352015271LGN00"}, {"date": "2015-10-14T18:03:13", "id": "LC8_L1T_TOA/LC80370352015287LGN00"}, {"date": "2015-10-30T18:03:19", "id": "LC8_L1T_TOA/LC80370352015303LGN00"}, {"date": "2015-11-15T18:03:19", "id": "LC8_L1T_TOA/LC80370352015319LGN00"}, {"date": "2015-12-01T18:03:21", "id": "LC8_L1T_TOA/LC80370352015335LGN00"}, {"date": "2015-12-17T18:03:21", "id": "LC8_L1T_TOA/LC80370352015351LGN00"}, {"date": "2016-01-02T18:03:18", "id": "LC8_L1T_TOA/LC80370352016002LGN00"}, {"date": "2016-01-18T18:03:18", "id": "LC8_L1T_TOA/LC80370352016018LGN00"}, {"date": "2016-02-03T18:03:15", "id": "LC8_L1T_TOA/LC80370352016034LGN00"}, {"date": "2016-02-19T18:03:06", "id": "LC8_L1T_TOA/LC80370352016050LGN00"}, {"date": "2016-03-06T18:03:05", "id": "LC8_L1T_TOA/LC80370352016066LGN00"}, {"date": "2016-03-22T18:02:58", "id": "LC8_L1T_TOA/LC80370352016082LGN00"}, {"date": "2016-04-07T18:02:50", "id": "LC8_L1T_TOA/LC80370352016098LGN00"}, {"date": "2016-04-23T18:02:43", "id": "LC8_L1T_TOA/LC80370352016114LGN00"}, {"date": "2016-05-09T18:02:46", "id": "LC8_L1T_TOA/LC80370352016130LGN00"}, {"date": "2016-05-25T18:02:50", "id": "LC8_L1T_TOA/LC80370352016146LGN00"}, {"date": "2016-06-10T18:02:54", "id": "LC8_L1T_TOA/LC80370352016162LGN00"}, {"date": "2016-06-26T18:03:01", "id": "LC8_L1T_TOA/LC80370352016178LGN00"}, {"date": "2016-07-12T18:03:09", "id": "LC8_L1T_TOA/LC80370352016194LGN00"}, {"date": "2016-07-28T18:03:13", "id": "LC8_L1T_TOA/LC80370352016210LGN00"}, {"date": "2016-08-13T18:03:15", "id": "LC8_L1T_TOA/LC80370352016226LGN00"}, {"date": "2016-08-29T18:03:23", "id": "LC8_L1T_TOA/LC80370352016242LGN00"}, {"date": "2016-09-14T18:03:26", "id": "LC8_L1T_TOA/LC80370352016258LGN00"}, {"date": "2016-09-30T18:03:27", "id": "LC8_L1T_TOA/LC80370352016274LGN00"}, {"date": "2016-04-30T18:08:59", "id": "LC8_L1T_TOA/LC80380352016121LGN00"}, {"date": "2016-05-16T18:08:57", "id": "LC8_L1T_TOA/LC80380352016137LGN00"}]}
You need to sort results, not the object itself. The sort function or parameter has to be wrapped into an array for using _.sortBy.
sortedArray = _.sortBy(data.results, [function (a){
return a.date;
}]);
or
sortedArray = _.sortBy(data.results, ['date']);
var data = { count: 56, results: [{ date: "2014-06-05T18:02:51", id: "LC8_L1T_TOA/LC80370352014156LGN01" }, { date: "2014-06-21T18:02:55", id: "LC8_L1T_TOA/LC80370352014172LGN00" }, { date: "2014-07-07T18:03:03", id: "LC8_L1T_TOA/LC80370352014188LGN00" }, { date: "2014-07-23T18:03:05", id: "LC8_L1T_TOA/LC80370352014204LGN00" }, { date: "2014-08-08T18:03:14", id: "LC8_L1T_TOA/LC80370352014220LGN00" }, { date: "2014-08-24T18:03:17", id: "LC8_L1T_TOA/LC80370352014236LGN00" }, { date: "2014-09-09T18:03:21", id: "LC8_L1T_TOA/LC80370352014252LGN00" }, { date: "2014-09-25T18:03:18", id: "LC8_L1T_TOA/LC80370352014268LGN00" }, { date: "2014-10-11T18:03:25", id: "LC8_L1T_TOA/LC80370352014284LGN00" }, { date: "2014-10-27T18:03:23", id: "LC8_L1T_TOA/LC80370352014300LGN00" }, { date: "2014-11-12T18:03:26", id: "LC8_L1T_TOA/LC80370352014316LGN00" }, { date: "2014-11-28T18:03:24", id: "LC8_L1T_TOA/LC80370352014332LGN00" }, { date: "2014-12-14T18:03:18", id: "LC8_L1T_TOA/LC80370352014348LGN00" }, { date: "2014-12-30T18:03:14", id: "LC8_L1T_TOA/LC80370352014364LGN00" }, { date: "2015-01-15T18:03:14", id: "LC8_L1T_TOA/LC80370352015015LGN00" }, { date: "2015-01-31T18:03:10", id: "LC8_L1T_TOA/LC80370352015031LGN00" }, { date: "2015-02-16T18:03:01", id: "LC8_L1T_TOA/LC80370352015047LGN00" }, { date: "2015-03-04T18:02:56", id: "LC8_L1T_TOA/LC80370352015063LGN00" }, { date: "2015-03-20T18:02:46", id: "LC8_L1T_TOA/LC80370352015079LGN00" }, { date: "2015-04-05T18:02:35", id: "LC8_L1T_TOA/LC80370352015095LGN00" }, { date: "2015-04-21T18:02:34", id: "LC8_L1T_TOA/LC80370352015111LGN00" }, { date: "2015-05-07T18:02:18", id: "LC8_L1T_TOA/LC80370352015127LGN00" }, { date: "2015-05-23T18:02:16", id: "LC8_L1T_TOA/LC80370352015143LGN00" }, { date: "2015-06-08T18:02:26", id: "LC8_L1T_TOA/LC80370352015159LGN00" }, { date: "2015-06-24T18:02:32", id: "LC8_L1T_TOA/LC80370352015175LGN00" }, { date: "2015-07-10T18:02:43", id: "LC8_L1T_TOA/LC80370352015191LGN00" }, { date: "2015-07-26T18:02:49", id: "LC8_L1T_TOA/LC80370352015207LGN00" }, { date: "2015-08-11T18:02:53", id: "LC8_L1T_TOA/LC80370352015223LGN00" }, { date: "2015-08-27T18:03:00", id: "LC8_L1T_TOA/LC80370352015239LGN00" }, { date: "2015-09-12T18:03:07", id: "LC8_L1T_TOA/LC80370352015255LGN00" }, { date: "2015-09-28T18:03:13", id: "LC8_L1T_TOA/LC80370352015271LGN00" }, { date: "2015-10-14T18:03:13", id: "LC8_L1T_TOA/LC80370352015287LGN00" }, { date: "2015-10-30T18:03:19", id: "LC8_L1T_TOA/LC80370352015303LGN00" }, { date: "2015-11-15T18:03:19", id: "LC8_L1T_TOA/LC80370352015319LGN00" }, { date: "2015-12-01T18:03:21", id: "LC8_L1T_TOA/LC80370352015335LGN00" }, { date: "2015-12-17T18:03:21", id: "LC8_L1T_TOA/LC80370352015351LGN00" }, { date: "2016-01-02T18:03:18", id: "LC8_L1T_TOA/LC80370352016002LGN00" }, { date: "2016-01-18T18:03:18", id: "LC8_L1T_TOA/LC80370352016018LGN00" }, { date: "2016-02-03T18:03:15", id: "LC8_L1T_TOA/LC80370352016034LGN00" }, { date: "2016-02-19T18:03:06", id: "LC8_L1T_TOA/LC80370352016050LGN00" }, { date: "2016-03-06T18:03:05", id: "LC8_L1T_TOA/LC80370352016066LGN00" }, { date: "2016-03-22T18:02:58", id: "LC8_L1T_TOA/LC80370352016082LGN00" }, { date: "2016-04-07T18:02:50", id: "LC8_L1T_TOA/LC80370352016098LGN00" }, { date: "2016-04-23T18:02:43", id: "LC8_L1T_TOA/LC80370352016114LGN00" }, { date: "2016-05-09T18:02:46", id: "LC8_L1T_TOA/LC80370352016130LGN00" }, { date: "2016-05-25T18:02:50", id: "LC8_L1T_TOA/LC80370352016146LGN00" }, { date: "2016-06-10T18:02:54", id: "LC8_L1T_TOA/LC80370352016162LGN00" }, { date: "2016-06-26T18:03:01", id: "LC8_L1T_TOA/LC80370352016178LGN00" }, { date: "2016-07-12T18:03:09", id: "LC8_L1T_TOA/LC80370352016194LGN00" }, { date: "2016-07-28T18:03:13", id: "LC8_L1T_TOA/LC80370352016210LGN00" }, { date: "2016-08-13T18:03:15", id: "LC8_L1T_TOA/LC80370352016226LGN00" }, { date: "2016-08-29T18:03:23", id: "LC8_L1T_TOA/LC80370352016242LGN00" }, { date: "2016-09-14T18:03:26", id: "LC8_L1T_TOA/LC80370352016258LGN00" }, { date: "2016-09-30T18:03:27", id: "LC8_L1T_TOA/LC80370352016274LGN00" }, { date: "2016-04-30T18:08:59", id: "LC8_L1T_TOA/LC80380352016121LGN00" }, { date: "2016-05-16T18:08:57", id: "LC8_L1T_TOA/LC80380352016137LGN00" }] },
sortedArray = _.sortBy(data.results, ['date']);
console.log(sortedArray);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>
Sorting with Array#sort, in situ.
var data = { count: 56, results: [{ date: "2014-06-05T18:02:51", id: "LC8_L1T_TOA/LC80370352014156LGN01" }, { date: "2014-06-21T18:02:55", id: "LC8_L1T_TOA/LC80370352014172LGN00" }, { date: "2014-07-07T18:03:03", id: "LC8_L1T_TOA/LC80370352014188LGN00" }, { date: "2014-07-23T18:03:05", id: "LC8_L1T_TOA/LC80370352014204LGN00" }, { date: "2014-08-08T18:03:14", id: "LC8_L1T_TOA/LC80370352014220LGN00" }, { date: "2014-08-24T18:03:17", id: "LC8_L1T_TOA/LC80370352014236LGN00" }, { date: "2014-09-09T18:03:21", id: "LC8_L1T_TOA/LC80370352014252LGN00" }, { date: "2014-09-25T18:03:18", id: "LC8_L1T_TOA/LC80370352014268LGN00" }, { date: "2014-10-11T18:03:25", id: "LC8_L1T_TOA/LC80370352014284LGN00" }, { date: "2014-10-27T18:03:23", id: "LC8_L1T_TOA/LC80370352014300LGN00" }, { date: "2014-11-12T18:03:26", id: "LC8_L1T_TOA/LC80370352014316LGN00" }, { date: "2014-11-28T18:03:24", id: "LC8_L1T_TOA/LC80370352014332LGN00" }, { date: "2014-12-14T18:03:18", id: "LC8_L1T_TOA/LC80370352014348LGN00" }, { date: "2014-12-30T18:03:14", id: "LC8_L1T_TOA/LC80370352014364LGN00" }, { date: "2015-01-15T18:03:14", id: "LC8_L1T_TOA/LC80370352015015LGN00" }, { date: "2015-01-31T18:03:10", id: "LC8_L1T_TOA/LC80370352015031LGN00" }, { date: "2015-02-16T18:03:01", id: "LC8_L1T_TOA/LC80370352015047LGN00" }, { date: "2015-03-04T18:02:56", id: "LC8_L1T_TOA/LC80370352015063LGN00" }, { date: "2015-03-20T18:02:46", id: "LC8_L1T_TOA/LC80370352015079LGN00" }, { date: "2015-04-05T18:02:35", id: "LC8_L1T_TOA/LC80370352015095LGN00" }, { date: "2015-04-21T18:02:34", id: "LC8_L1T_TOA/LC80370352015111LGN00" }, { date: "2015-05-07T18:02:18", id: "LC8_L1T_TOA/LC80370352015127LGN00" }, { date: "2015-05-23T18:02:16", id: "LC8_L1T_TOA/LC80370352015143LGN00" }, { date: "2015-06-08T18:02:26", id: "LC8_L1T_TOA/LC80370352015159LGN00" }, { date: "2015-06-24T18:02:32", id: "LC8_L1T_TOA/LC80370352015175LGN00" }, { date: "2015-07-10T18:02:43", id: "LC8_L1T_TOA/LC80370352015191LGN00" }, { date: "2015-07-26T18:02:49", id: "LC8_L1T_TOA/LC80370352015207LGN00" }, { date: "2015-08-11T18:02:53", id: "LC8_L1T_TOA/LC80370352015223LGN00" }, { date: "2015-08-27T18:03:00", id: "LC8_L1T_TOA/LC80370352015239LGN00" }, { date: "2015-09-12T18:03:07", id: "LC8_L1T_TOA/LC80370352015255LGN00" }, { date: "2015-09-28T18:03:13", id: "LC8_L1T_TOA/LC80370352015271LGN00" }, { date: "2015-10-14T18:03:13", id: "LC8_L1T_TOA/LC80370352015287LGN00" }, { date: "2015-10-30T18:03:19", id: "LC8_L1T_TOA/LC80370352015303LGN00" }, { date: "2015-11-15T18:03:19", id: "LC8_L1T_TOA/LC80370352015319LGN00" }, { date: "2015-12-01T18:03:21", id: "LC8_L1T_TOA/LC80370352015335LGN00" }, { date: "2015-12-17T18:03:21", id: "LC8_L1T_TOA/LC80370352015351LGN00" }, { date: "2016-01-02T18:03:18", id: "LC8_L1T_TOA/LC80370352016002LGN00" }, { date: "2016-01-18T18:03:18", id: "LC8_L1T_TOA/LC80370352016018LGN00" }, { date: "2016-02-03T18:03:15", id: "LC8_L1T_TOA/LC80370352016034LGN00" }, { date: "2016-02-19T18:03:06", id: "LC8_L1T_TOA/LC80370352016050LGN00" }, { date: "2016-03-06T18:03:05", id: "LC8_L1T_TOA/LC80370352016066LGN00" }, { date: "2016-03-22T18:02:58", id: "LC8_L1T_TOA/LC80370352016082LGN00" }, { date: "2016-04-07T18:02:50", id: "LC8_L1T_TOA/LC80370352016098LGN00" }, { date: "2016-04-23T18:02:43", id: "LC8_L1T_TOA/LC80370352016114LGN00" }, { date: "2016-05-09T18:02:46", id: "LC8_L1T_TOA/LC80370352016130LGN00" }, { date: "2016-05-25T18:02:50", id: "LC8_L1T_TOA/LC80370352016146LGN00" }, { date: "2016-06-10T18:02:54", id: "LC8_L1T_TOA/LC80370352016162LGN00" }, { date: "2016-06-26T18:03:01", id: "LC8_L1T_TOA/LC80370352016178LGN00" }, { date: "2016-07-12T18:03:09", id: "LC8_L1T_TOA/LC80370352016194LGN00" }, { date: "2016-07-28T18:03:13", id: "LC8_L1T_TOA/LC80370352016210LGN00" }, { date: "2016-08-13T18:03:15", id: "LC8_L1T_TOA/LC80370352016226LGN00" }, { date: "2016-08-29T18:03:23", id: "LC8_L1T_TOA/LC80370352016242LGN00" }, { date: "2016-09-14T18:03:26", id: "LC8_L1T_TOA/LC80370352016258LGN00" }, { date: "2016-09-30T18:03:27", id: "LC8_L1T_TOA/LC80370352016274LGN00" }, { date: "2016-04-30T18:08:59", id: "LC8_L1T_TOA/LC80380352016121LGN00" }, { date: "2016-05-16T18:08:57", id: "LC8_L1T_TOA/LC80380352016137LGN00" }] };
data.results.sort(function (a, b) {
return a.date.localeCompare(b.date);
});
console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }
var sampleData = {
"count": 56,
"results": [{
"date": "2015-06-05T18:02:51",
"id": "1"
},
{
"date": "2014-06-21T18:02:55",
"id": "2"
},
{
"date": "2016-07-07T18:03:03",
"id": "3"
},
{
"date": "2008-07-23T18:03:05",
"id": "4"
}
]
};
_.sortBy(sampleData.results, 'date');
There was one quote less in object line 3
Related
const minutes = [
{
value: "00",
id: 1,
},
{
id: 2,
value: "05",
},
{
id: 3,
value: "10",
},
{
id: 4,
value: "15",
},
{
id: 5,
value: "20",
},
{
id: 6,
value: "25",
},
{
id: 7,
value: "30",
},
{
id: 8,
value: "35",
},
{
id: 9,
value: "40",
},
{
id: 10,
value: "45",
},
{
id: 11,
value: "50",
},
{
id: 12,
value: "55",
},
];
How can i make like this dynamically
From luk2302's comment to implement,
let arr = []
for (let i = 0; i < 12; i++) {
arr.push({
id: (i + 1).toString(),
value: (i * 5).toString()
})
}
console.log(arr)
I have object like this.
data = [
{
id: "0",
name: "maths",
levelNo: 0,
level: null
},
{
id: "15",
name: "sceince",
levelNo: 0,
level: null
},
{
name: "algebra",
id: "1",
parentId: "0",
levelNo: 1,
level: {
id: "0",
name: "maths"
}
},
{
name: "alfunction",
id: "2",
parentId: "1",
levelNo: 2,
level: {
id: "1",
name: "alegera"
}
},
{
name: "bhumiti",
id: "3",
parentId: "1",
levelNo: 2,
level: {
id: "1",
name: "alegera"
}
},
{
name: "paryavan",
id: "4",
parentId: "0",
levelNo: 1,
level: {
id: "0",
name: "maths"
}
},
{
name: "trikon",
id: "5",
parentId: "3",
levelNo: 3,
level: {
id: "3",
name: "bhumiti"
}
}];
and convert this object into
subject = [
{
name: "maths",
id: "0",
items: [
{
id: "1",
name: "alegera",
items: [
{
name: "alfunction",
id: "2"
},
{
name: "bhumiti",
id: "3",
items: [
{
name: "trikon",
id: "5"
}
]
}
]
},
{
id: "4",
name: "paryavan"
}
]
}];
You could take a function which uses parentId and id without levelNo and level.
const
getTree = (data, root) => {
const t = {};
data.forEach(({ parentId, levelNo, level, ...o }) =>
((t[parentId] ??= {}).children ??= []).push(Object.assign(t[o.id] ??= {}, o))
);
return t[root].children;
},
data = [{ id: "0", name: "maths", levelNo: 0, level: null }, { id: "15", name: "sceince", levelNo: 0, level: null }, { name: "algebra", id: "1", parentId: "0", levelNo: 1, level: { id: "0", name: "maths" } }, { name: "alfunction", id: "2", parentId: "1", levelNo: 2, level: { id: "1", name: "alegera" } }, { name: "bhumiti", id: "3", parentId: "1", levelNo: 2, level: { id: "1", name: "alegera" } }, { name: "paryavan", id: "4", parentId: "0", levelNo: 1, level: { id: "0", name: "maths" } }, { name: "trikon", id: "5", parentId: "3", levelNo: 3, level: { id: "3", name: "bhumiti" } }],
tree = getTree(data);
console.log(tree);
.as-console-wrapper { max-height: 100% !important; top: 0; }
I have an array and I want to override the object attributes
This the main data
const Data = {
"id": "1",
"name": "USA",
"questions": [{ id: 1, name: "1 qst" }, { id: 2, name: "2 qst" }, { id: 3, name: "3 qst" }],
"children": [
{ "id": "1" , "name": "DC" ,"questions": [{ id: 1, name: "1 qst" }, { id: 2, name: "2 qst" }, { id:2, name: "3 qst" }]},
{ "id": "2" , "name": "Florida" ,"questions": [{ id: 1, name: "1 qst" }, { id: 2, name: "2 qst" }, { id: 3, name: "3 qst" }]}
]
}
I want to change in every question instead of name I want to put questionName like this
{ id: 1, questionName: "1 qst" }
I was able to change it in first object question through this code
let dataFiltred = Data[0]?.questions?.map((item) => {
return {
questionName: item.name,
id: item.id,
}
})
But I am struggling to change it in children question
function mapQuestionObject({ name, id }) {
return { id, questionName: name };
}
const mapped = {
...Data,
questions: Data.questions.map(mapQuestionObject),
children: Data.children.map(child => ({
...child,
questions: child.questions.map(mapQuestionObject),
}),
};
Map each questions array to a new array and change the name property in the mapped value.
const data = {
"id": "1",
"name": "USA",
"questions": [{ id: 1, name: "1 qst" }, { id: 2, name: "2 qst" }, { id: 3, name: "3 qst" }],
"children": [
{ "id": "1" , "name": "DC" ,"questions": [{ id: 1, name: "1 qst" }, { id: 2, name: "2 qst" }, { id:2, name: "3 qst" }]},
{ "id": "2" , "name": "Florida" ,"questions": [{ id: 1, name: "1 qst" }, { id: 2, name: "2 qst" }, { id: 3, name: "3 qst" }]}
]
};
const newData = {
...data,
questions: data.questions.map(({ name: questionName, ...rest }) => ({
...rest,
questionName,
})),
children: data.children.map(child => ({
...child,
questions: child.questions.map(({ name: questionName, ...rest }) => ({
...rest,
questionName,
}))
})),
};
console.log(newData);
Since the questions mapping is the same callback you can factor it out to make your code more DRY
const data = {
"id": "1",
"name": "USA",
"questions": [{ id: 1, name: "1 qst" }, { id: 2, name: "2 qst" }, { id: 3, name: "3 qst" }],
"children": [
{ "id": "1" , "name": "DC" ,"questions": [{ id: 1, name: "1 qst" }, { id: 2, name: "2 qst" }, { id:2, name: "3 qst" }]},
{ "id": "2" , "name": "Florida" ,"questions": [{ id: 1, name: "1 qst" }, { id: 2, name: "2 qst" }, { id: 3, name: "3 qst" }]}
]
};
const mapQuestions = arr => arr.map(({ name: questionName, ...rest }) => ({
...rest,
questionName,
}));
const newData = {
...data,
questions: mapQuestions(data.questions),
children: data.children.map(child => ({
...child,
questions: mapQuestions(child.questions),
})),
};
console.log(newData);
how can i convert returned data from this structure:
[
{
"id": 91,
"name": "Art",
"division": {
"id": 4,
"name": "1st level",
"created_at": "2018-11-05T10:11:37+00:00",
},
"created_at": "2018-11-05T10:11:37+00:00",
},
{
"id": 188,
"name": "English",
"division": {
"id": 9,
"name": "3rd level",
"created_at": "2018-11-05T10:11:37+00:00",
},
"created_at": "2018-11-05T10:11:37+00:00",
},
{
"id": 218,
"name": "Art",
"division": {
"id": 11,
"name": "3rd level",
"created_at": "2018-11-05T10:11:37+00:00",
},
"created_at": "2018-11-05T10:11:37+00:00",
}
]
to this structure :
[
{
"id": 1,
"name": "Art",
"classes": [
{
"id": 91,
"name": "1st level",
},
{
"id": 218,
"name": "3rd level",
},
],
},
{
"id": 2,
"name": "English",
"classes": [
{
"id": 188,
"name": "3rd level",
},
],
},
]
note: class.id = parentSubject.id
I wrote some codes to solve the problem but I'm looking for the best way !!
i use .reduce() function
I will attach the codes in the comments box.
Here is how I would do it:
let ans = initialArray.reduce((cum,x) => {
let foundIndex = cum.findIndex((a) =>{
return a.name == x.name});
if(foundIndex!=-1){
cum[foundIndex].classes.push({
id : x.id,
name : x.division.name
})
}
else{
cum.push({
id : cum.length+1,
name : x.name,
classes : [{
id : x.id,
name : x.division.name
}]
})
}
return cum;
},[]);
I use reduce and findIndex methods.
You can use array map to create a new array of objects with the new id.
const oldDatas = [
{
id: 91,
name: 'Art',
division: {
id: 4,
name: '1st level',
created_at: '2018-11-05T10:11:37+00:00',
},
created_at: '2018-11-05T10:11:37+00:00',
},
{
id: 188,
name: 'English',
division: {
id: 9,
name: '3rd level',
created_at: '2018-11-05T10:11:37+00:00',
},
created_at: '2018-11-05T10:11:37+00:00',
},
{
id: 218,
name: 'Art',
division: {
id: 11,
name: '3rd level',
created_at: '2018-11-05T10:11:37+00:00',
},
created_at: '2018-11-05T10:11:37+00:00',
},
];
const newDatas = oldDatas.map((data, index) => {
return { ...data, id: index + 1 };
});
console.log(newDatas);
my solution :
let res = initialArray.reduce((acc, obj) => {
const exist = acc.findIndex((item) => item.name === obj.name);
if (exist >= 0) {
acc[exist] = {
id: exist,
name: obj.name,
classes: [
...acc[exist].classes,
{ id: obj.id, name: obj.division.name },
],
};
} else {
acc.push({
id: acc.length,
name: obj.name,
classes: [{ id: obj.id, name: obj.division.name }],
});
}
return acc;
}, []);
I have input data structure like
const i = [
{ Time: "Breakfast", City: "Bangalore", Area: "Bellandur", count: 12 },
{ Time: "Breakfast", City: "Hyderabad", Area: "LBNagar", count: 19 },
{ Time: "Lunch", City: "Bangalore", Area: "Koramangala", count: 22 },
{ Time: "Dinner", City: "Hyderabad", Area: "Koti", count: 10 },
{ Time: "Dinner", City: "Bangalore", Area: "Bellandur", count: 16 },
{ Time: "Lunch", City: "Hyderabad", Area: "LBNagar", count: 28 },
{ Time: "Breakfast", City: "Bangalore", Area: "Bellandur", count: 11 },
{ Time: "Lunch", City: "Hyderabad", Area: "Koti", count: 24 },
{ Time: "Dinner", City: "Bangalore", Area: "Koramangala", count: 27 },
{ Time: "Breakfast", City: "Bangalore", Area: "Bellandur", count: 22 },
{ Time: "Breakfast", City: "Hyderabad", Area: "Manikonda", count: 11 },
{ Time: "Dinner", City: "Hyderabad", Area: "Manikonda", count: 10 },
{ Time: "Lunch", City: "Bangalore", Area: "Bellandur", count: 17 }
];
Expected transformed data structure should be nested array with count prop equals to sum of children's count value
[
{
"d": "Breakfast",
"count": 42,
"children": [
{
"d": "Bangalore",
"count": 12,
"children": [
{
"d": "Bellandur",
"count": 12,
"children": []
}
]
},
{
"d": "Hyderabad",
"count": 30,
"children": [
{
"d": "LBNagar",
"count": 19,
"children": []
},
{
"d": "Manikonda",
"count": 11,
"children": []
}
]
}
]
},
{
"d": "Lunch",
"count": 91,
"children": [
{
"d": "Bangalore",
"count": 39,
"children": [
{
"d": "Koramangala",
"count": 22,
"children": []
},
{
"d": "Bellandur",
"count": 17,
"children": []
}
]
},
{
"d": "Hyderabad",
"count": 52,
"children": [
{
"d": "LBNagar",
"count": 28,
"children": []
},
{
"d": "Koti",
"count": 24,
"children": []
}
]
}
]
},
{
"d": "Dinner",
"count": 63,
"children": [
{
"d": "Hyderabad",
"count": 20,
"children": [
{
"d": "Koti",
"count": 10,
"children": []
},
{
"d": "Manikonda",
"count": 10,
"children": []
}
]
},
{
"d": "Bangalore",
"count": 43,
"children": [
{
"d": "Bellandur",
"count": 16,
"children": []
},
{
"d": "Koramangala",
"count": 27,
"children": []
}
]
}
]
}
]
To achieve above result, I have snippet
const input = [
{ Time: "Breakfast", City: "Bangalore", Area: "Bellandur", count: 12 },
{ Time: "Breakfast", City: "Hyderabad", Area: "LBNagar", count: 19 },
{ Time: "Lunch", City: "Bangalore", Area: "Koramangala", count: 22 },
{ Time: "Dinner", City: "Hyderabad", Area: "Koti", count: 10 },
{ Time: "Dinner", City: "Bangalore", Area: "Bellandur", count: 16 },
{ Time: "Lunch", City: "Hyderabad", Area: "LBNagar", count: 28 },
{ Time: "Breakfast", City: "Bangalore", Area: "Bellandur", count: 11 },
{ Time: "Lunch", City: "Hyderabad", Area: "Koti", count: 24 },
{ Time: "Dinner", City: "Bangalore", Area: "Koramangala", count: 27 },
{ Time: "Breakfast", City: "Bangalore", Area: "Bellandur", count: 22 },
{ Time: "Breakfast", City: "Hyderabad", Area: "Manikonda", count: 11 },
{ Time: "Dinner", City: "Hyderabad", Area: "Manikonda", count: 10 },
{ Time: "Lunch", City: "Bangalore", Area: "Bellandur", count: 17 }
];
// convert nested array into map
// Ex: {Breakfast: { Bangalore: {Bellandur: 40 }, ... } ... }}}
const nestedMap = input.reduce((acc, v) => {
if (!acc[v["Time"]]) {
acc[v["Time"]] = {};
}
if (!acc[v["Time"]][v["City"]]) {
acc[v["Time"]][v["City"]] = {};
}
if (!acc[v["Time"]][v["City"]][v["Area"]]) {
acc[v["Time"]][v["City"]][v["Area"]] = v["count"];
}
return acc;
}, {});
const summer = (o, i) =>
typeof o === "number"
? i + o
: Object.values(o).reduce((acc, v) => acc + summer(v, i), 0);
const aggregator = o => {
return typeof o === "number"
? []
: Object.entries(o).map(([n, child]) => ({
d: n,
count: summer(child, 0),
children: aggregator(child)
}));
};
const result = aggregator(nestedMap);
console.log(JSON.stringify(result, undefined, 2));
Thought my snippet working perfectly, It involves lot of iterations. First converting into map and one iteration (summer function) to get count of children and other iteration (aggregator function) to form nested array.
I am looking for better performant solution.
Thanks in advance!
You could take a more concise approach with an array of the nested keys and seach for the object with the value at the level.
Then add count and return the actual object.
This approach prevents tailing children arrays
var data = [{ Time: "Breakfast", City: "Bangalore", Area: "Bellandur", count: 12 }, { Time: "Breakfast", City: "Hyderabad", Area: "LBNagar", count: 19 }, { Time: "Lunch", City: "Bangalore", Area: "Koramangala", count: 22 }, { Time: "Dinner", City: "Hyderabad", Area: "Koti", count: 10 }, { Time: "Dinner", City: "Bangalore", Area: "Bellandur", count: 16 }, { Time: "Lunch", City: "Hyderabad", Area: "LBNagar", count: 28 }, { Time: "Breakfast", City: "Bangalore", Area: "Bellandur", count: 11 }, { Time: "Lunch", City: "Hyderabad", Area: "Koti", count: 24 }, { Time: "Dinner", City: "Bangalore", Area: "Koramangala", count: 27 }, { Time: "Breakfast", City: "Bangalore", Area: "Bellandur", count: 22 }, { Time: "Breakfast", City: "Hyderabad", Area: "Manikonda", count: 11 }, { Time: "Dinner", City: "Hyderabad", Area: "Manikonda", count: 10 }, { Time: "Lunch", City: "Bangalore", Area: "Bellandur", count: 17 }],
keys = ['Time', 'City', 'Area'],
result = data
.reduce((r, o) => {
keys.reduce((p, k) => {
var temp = (p.children = p.children || []).find(q => q.d === o[k]);
if (!temp) p.children.push(temp = { d: o[k], count: 0 });
temp.count += o.count;
return temp;
}, r);
return r;
}, { children: [] })
.children;
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }