I want to remove duplicates from an array of objects can anyone help with this to remove duplicates in the array of objects. I have an array of array of objects that I want to remove duplicates and I want to use
const firstArray = [
{
first: "01",
data: [
{ id: "01", name: "test1" },
{ id: "02", name: "test2" },
{ id: "03", name: "test3" },
{ id: "05", name: "test1" },
{ id: "06", name: "test2" },
{ id: "07", name: "test1" }
]
},
{
first: "02",
data: [
{ id: "01", name: "test2" },
{ id: "02", name: "test1" },
{ id: "03", name: "test3" },
{ id: "05", name: "test2" },
{ id: "06", name: "test2" },
{ id: "07", name: "test1" }
]
},
{
first: "03",
data: [
{ id: "01", name: "test3" },
{ id: "02", name: "test2" },
{ id: "03", name: "test3" },
{ id: "04", name: "test2" },
{ id: "05", name: "test3" },
{ id: "07", name: "test1" }
]
}
];
this is the sample result code i am expecting :
const firstArray = [
{
first: "01",
data: [
{ id: "01", name: "test1" },
{ id: "02", name: "test2" },
{ id: "03", name: "test3" },
]
},
{
first: "02",
data: [
{ id: "01", name: "test1" },
{ id: "02", name: "test2" },
{ id: "03", name: "test3" },
]
},
{
first: "03",
data: [
{ id: "05", name: "test1" },
{ id: "06", name: "test2" },
{ id: "07", name: "test3" }
]
}
];```
this way..
const firstArray =
[ { first: '01'
, data:
[ { id: '01', name: 'test1' }
, { id: '02', name: 'test2' }
, { id: '03', name: 'test3' }
, { id: '05', name: 'test1' }
, { id: '06', name: 'test2' }
, { id: '07', name: 'test1' }
] }
, { first: '02'
, data:
[ { id: '01', name: 'test2' }
, { id: '02', name: 'test1' }
, { id: '03', name: 'test3' }
, { id: '05', name: 'test2' }
, { id: '06', name: 'test2' }
, { id: '07', name: 'test1' }
] }
, { first: '03'
, data:
[ { id: '01', name: 'test3' }
, { id: '02', name: 'test2' }
, { id: '03', name: 'test3' }
, { id: '04', name: 'test2' }
, { id: '05', name: 'test3' }
, { id: '07', name: 'test1' }
] } ]
firstArray.forEach(({data})=>
{
for (let i=data.length;--i>0;)
if (data.findIndex(({name})=>name===data[i].name) < i)
data.splice(i,1)
})
console.log( JSON.stringify( firstArray,0,2))
.as-console-wrapper {max-height: 100%!important;top:0 }
const firstArray = [
{
first: "01",
data: [
{ id: "01", name: "test1" },
{ id: "02", name: "test2" },
{ id: "03", name: "test3" },
{ id: "05", name: "test1" },
{ id: "06", name: "test2" },
{ id: "07", name: "test1" }
]
},
{
first: "02",
data: [
{ id: "01", name: "test2" },
{ id: "02", name: "test1" },
{ id: "03", name: "test3" },
{ id: "05", name: "test2" },
{ id: "06", name: "test2" },
{ id: "07", name: "test1" }
]
},
{
first: "03",
data: [
{ id: "01", name: "test3" },
{ id: "02", name: "test2" },
{ id: "03", name: "test3" },
{ id: "04", name: "test2" },
{ id: "05", name: "test3" },
{ id: "07", name: "test1" }
]
}
];
let filterValues = firstArray.map((item) => {
let data = item.data.filter(
(value, index, data) =>
data.findIndex((item) => item.name === value.name) === index
);
return { ...{ first: item.first }, data };
});
console.log(filterValues);
Related
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 array of objects like below. Here children element may deeply nested, it may contain other children element as well.
let response = [{
id: 4321,
name: 'Education',
parentId: null,
children: [{
id: 1234,
name: 'category1',
parentId: 4321,
children: [{
id: 8327548,
name: '001',
parentId: 1234,
}, {
id: 8327549,
name: '002',
parentId: 1234,
}],
}, {
id: 6786,
name: 'Associations',
parentId: 4321,
}, {
id: 8262439,
name: 'category1',
parentId: 4321,
}, {
id: 8245,
name: 'Rights',
parentId: 4321,
children: [{
id: 2447,
name: 'Organizations',
parentId: 8245,
}, {
id: 9525,
name: 'Services',
parentId: 8245,
}, {
id: 8448,
name: 'Organizations',
parentId: 8245,
}],
}, {
id: 8262446,
name: 'Women\'s Rights',
parentId: 4321,
}],
}, {
id: 21610,
name: 'Agriculture',
parentId: null,
children: [{
id: 3302,
name: 'categoryABC',
parentId: 21610,
children: [{
id: 85379,
name: 'categoryABC - General',
parentId: 3302,
}, {
id: 85380,
name: 'categoryABC Technology',
parentId: 3302,
}],
}, {
id: 8303,
name: 'Fungicides',
parentId: 21610,
children: [{
id: 8503,
name: 'Fungicides - General',
parentId: 8303,
}],
}],
}];
I want to make it flat array of objects but I want to add parent name (which parentId is null) to every objects inside that respective parent.
expected output:
[
{
id: 8327548,
name: "001",
parentId: 1234
mainParent: "Education"
}, {
id: 8327549,
name: "002",
parentId: 1234,
mainParent: "Agriculture"
},
// ...OTHER OBJECTS....
]
What I have done so far
function flat(array) {
var result = [];
array.forEach(function (a) {
result.push(a);
if (Array.isArray(a.children)) {
result = result.concat(flat(a.children));
delete a.children;
}
});
return result;
}
It's giving the flat array of objects but I'm not able to add parent name property to every objects.
Can someone please help me?
You could take a recursive approach an handover the first found name as mainParent.
const
flat = mainParent => o => o.children
? o.children.flatMap(flat(mainParent || o.name))
: { ...o, mainParent },
response = [{ id: 4321, name: "Education", parentId: null, children: [{ id: 1234, name: "category1", parentId: 4321, children: [{ id: 8327548, name: "001", parentId: 1234 }, { id: 8327549, name: "002", parentId: 1234 }] }, { id: 6786, name: "Associations", parentId: 4321 }, { id: 8262439, name: "category1", parentId: 4321 }, { id: 8245, name: "Rights", parentId: 4321, children: [{ id: 2447, name: "Organizations", parentId: 8245 }, { id: 9525, name: "Services", parentId: 8245 }, { id: 8448, name: "Organizations", parentId: 8245 }] }, { id: 8262446, name: "Women's Rights", parentId: 4321 }] }, { id: 21610, name: "Agriculture", parentId: null, children: [{ id: 3302, name: "categoryABC", parentId: 21610, children: [{ id: 85379, name: "categoryABC - General", parentId: 3302 }, { id: 85380, name: "categoryABC Technology", parentId: 3302 }] }, { id: 8303, name: "Fungicides", parentId: 21610, children: [{ id: 8503, name: "Fungicides - General", parentId: 8303 }] }] }],
result = response.flatMap(flat());
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
This is a fairly clean recursion, stopping when there are no children in the node. We capture the first name value found along the hierarchy and carry it through.
const flat = (xs, name = null) =>
xs .flatMap (x => x .children
? flat (x .children, name || x .name)
: [{...x, mainParent: name}]
)
const response = [{id: 4321, name: "Education", parentId: null, children: [{id: 1234, name: "category1", parentId: 4321, children: [{id: 8327548, name: "001", parentId: 1234}, {id: 8327549, name: "002", parentId: 1234}]}, {id: 6786, name: "Associations", parentId: 4321}, {id: 8262439, name: "category1", parentId: 4321}, {id: 8245, name: "Rights", parentId: 4321, children: [{id: 2447, name: "Organizations", parentId: 8245}, {id: 9525, name: "Services", parentId: 8245}, {id: 8448, name: "Organizations", parentId: 8245}]}, {id: 8262446, name: "Women's Rights", parentId: 4321}]}, {id: 21610, name: "Agriculture", parentId: null, children: [{id: 3302, name: "categoryABC", parentId: 21610, children: [{id: 85379, name: "categoryABC - General", parentId: 3302}, {id: 85380, name: "categoryABC Technology", parentId: 3302}]}, {id: 8303, name: "Fungicides", parentId: 21610, children: [{id: 8503, name: "Fungicides - General", parentId: 8303}]}]}]
console .log (flat (response))
.as-console-wrapper {max-height: 100% !important; top: 0}
Less obscure, but still fairly concise.
const response = [{ id: 4321, name: 'Education', parentId: null, children: [{ id: 1234, name: 'category1', parentId: 4321, children: [{ id: 8327548, name: '001', parentId: 1234 }, { id: 8327549, name: '002', parentId: 1234 }] }, { id: 6786, name: 'Associations', parentId: 4321 }, { id: 8262439, name: 'category1', parentId: 4321 }, { id: 8245, name: 'Rights', parentId: 4321, children: [{ id: 2447, name: 'Organizations', parentId: 8245 }, { id: 9525, name: 'Services', parentId: 8245 }, { id: 8448, name: 'Organizations', parentId: 8245 }] }, { id: 8262446, name: 'Women\'s Rights', parentId: 4321 }] }, { id: 21610, name: 'Agriculture', parentId: null, children: [{ id: 3302, name: 'categoryABC', parentId: 21610, children: [{ id: 85379, name: 'categoryABC - General', parentId: 3302 }, { id: 85380, name: 'categoryABC Technology', parentId: 3302 }] }, { id: 8303, name: 'Fungicides', parentId: 21610, children: [{ id: 8503, name: 'Fungicides - General', parentId: 8303 }] }] }];
function flatten(arr, mainParent = null) {
if (!arr) return;
let result = [];
for (const obj of arr) {
result.push(...(flatten(obj.children, mainParent ?? obj.name) ?? [{ ...obj, mainParent }]));
}
return result;
}
console.log(flatten(response));
.as-console-wrapper { max-height: 100% !important; top: 0; }
A possible solution could be based entirely on a single, though recursively implemented, reduce task, where the accumulating and recursively passed down object carries both information, the mainParent value and the recursively collected final result ...
function recursivelyReassambleAndCollectChildlessItems(
{ mainParent, result }, { children, ...item }
) {
result = result.concat(children && children.reduce(
recursivelyReassambleAndCollectChildlessItems, {
// was:
// mainParent: mainParent ?? item.name,
/**
* OP quote:
* "... but I want to add parent name
* (which parentId is null) to every
* objects inside that respective parent."
*/
mainParent: item.parentId === null ? item.name : mainParent,
result: [],
}
).result || { mainParent, ...item });
return { mainParent, result };
}
const response = [{
id: 4321,
name: 'Education',
parentId: null,
children: [{
id: 1234,
name: 'category1',
parentId: 4321,
children: [{
id: 8327548,
name: '001',
parentId: 1234,
}, {
id: 8327549,
name: '002',
parentId: 1234,
}],
}, {
id: 6786,
name: 'Associations',
parentId: 4321,
}, {
id: 8262439,
name: 'category1',
parentId: 4321,
}, {
id: 8245,
name: 'Rights',
parentId: 4321,
children: [{
id: 2447,
name: 'Organizations',
parentId: 8245,
}, {
id: 9525,
name: 'Services',
parentId: 8245,
}, {
id: 8448,
name: 'Organizations',
parentId: 8245,
}],
}, {
id: 8262446,
name: 'Women\'s Rights',
parentId: 4321,
}],
}, {
id: 21610,
name: 'Agriculture',
parentId: null,
children: [{
id: 3302,
name: 'categoryABC',
parentId: 21610,
children: [{
id: 85379,
name: 'categoryABC - General',
parentId: 3302,
}, {
id: 85380,
name: 'categoryABC Technology',
parentId: 3302,
}],
}, {
id: 8303,
name: 'Fungicides',
parentId: 21610,
children: [{
id: 8503,
name: 'Fungicides - General',
parentId: 8303,
}],
}],
}];
console.log(response
.reduce(recursivelyReassambleAndCollectChildlessItems, { result: [] })
.result
)
.as-console-wrapper { min-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);
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
Please help me, I am stuck with aggregates for kendoTreeList -
I create kendoTreeList, but failed to calculate sum for groups.
$(document).ready(function () {
var _jsondata = [
{ ID: 1, Name: "Parent 1", Amount: "200", parentId: null },
{ ID: 2, Name: "Parent 2", Amount: "500", parentId: null },
{ ID: 11, Name: "Child 11", Amount: "50", parentId: 1 },
{ ID: 12, Name: "Child 12", Amount: "150", parentId: 1 },
{ ID: 21, Name: "Child 21", Amount: "100", parentId: 2 },
{ ID: 22, Name: "Child 22", Amount: "200", parentId: 2 },
{ ID: 23, Name: "Child 23", Amount: "200", parentId: 2 }
];
var dataSource = new kendo.data.TreeListDataSource({
data : _jsondata,
schema : { model: { id: "ID", expanded: true }},
aggregate : [
{ field: "Amount", aggregate: "sum"}
]
});
$("#treelist").kendoTreeList({
dataSource : dataSource,
columns : [
{ field: "Name" , nullable: false },
{ field: "Amount", footerTemplate: "#= sum #"}
]
});
});
this give result as -
Since Amount represented in _jsondata as string, you need to define it in schema.model as number
schema: {
model: {
id: "ID",
expanded: true,
fields: {
Amount: { type: 'number' }
}
}
},