0👍
✅
The below code will give you an array with unique ‘Id_plane’ by keeping the first object data.
let data = [
{
DateCreated: "2020-10-10",
Ends: 15.5,
Id_plane: 1,
Id_sale: 4,
Id_scheduler: 6,
Starts: 7,
__isset_bitfield: 31
},
{
DateCreated: "2020-10-11",
Ends: 17,
Id_plane: 1,
Id_sale: 4,
Id_scheduler: 6,
Starts: 7,
__isset_bitfield: 31
},
{
DateCreated: "2020-10-12",
Ends: 17,
Id_plane: 2,
Id_sale: 4,
Id_scheduler: 6,
Starts: 4.2,
__isset_bitfield: 40
},
{
DateCreated: "2020-10-11",
Ends: 17,
Id_plane: 2,
Id_sale: 4,
Id_scheduler: 6,
Starts: 7,
__isset_bitfield: 31
},
];
data.map((item, index) => {
let matchingItems = data.filter(el => el.Id_plane === item.Id_plane);
matchingItems.map(matchingItem => {
let key = data.indexOf(matchingItem)
if (key !== index) {
data.splice(key, 1);
}
});
});
console.log(data);
0👍
From what I understood you want to return a new list based on the id_plane.
let list = [
{
id_plane: 1,
date_created: "2020-10-10"
},
{
id_plane: 1,
date_created: "2020-10-11"
},
{
id_plane: 4,
date_created: "2020-10-12"
},
{
id_plane: 10,
date_created: "2020-10-13"
},
{
id_plane: 1,
date_created: "2020-10-14"
},
{
id_plane: 4,
date_created: "2020-10-15"
}
];
var ids = list.map(e => e.id_plane).filter((x, i, a) => a.indexOf(x) == i);
var arr = {};
ids.forEach(i => {
arr[i] = list.filter(e => {
return e.id_plane === i;
});
});
console.log(arr);
Source:stackexchange.com