const objects = [...];
const ids = [1,2,3,8,9];
const missed = objects.filter(obj => !ids.includes(obj.id));
// проиндексируем по ключам
const [rootIndex, idIndex] = array.reduce(([roots, ids], obj) => {
(roots[obj.root_id] ??= []).push(obj);
ids[obj.id] = obj;
return [roots, ids];
}, [{}, {}]);
// и найдем по нему нужные объекты:
const objectsWithoutExistingParent = Object.keys(rootIndex).filter(key => !idIndex.hasOwnProperty(key)).flatMap(key => rootIndex[key]);
console.log(objectsWithoutExistingParent);
const roots = arr.filter((n, i, a) => !a.some(m => m.id === n.root_id));
const roots = arr.filter(function(n) {
return !this.has(n.root_id);
}, new Set(arr.map(n => n.id)));
const mapIds = {}
const filtered = array.filter(elem => {
const existingElement = mapIds[elem.id]
if (!existingElement)
mapIds[elem.id] = elem
else
existingElement.array1.push(...elem.array1)
return !existingElement
})
const mapIds = {}
const filtered = array.filter(elem =>
(!mapIds[elem.id] && (mapIds[elem.id] = elem)) || !mapIds[elem.id].array1.push(...elem.array1)
)
const key = 'workplace';
const values = [ 'office', 'hotel' ];
const result = arr.filter(n => values.includes(n[key]));
// или
const result = values.flatMap(function(n) {
return this[n] ?? [];
}, arr.reduce((acc, n) => ((acc[n[key]] = acc[n[key]] ?? []).push(n), acc), {}));
// или
const filter = (arr, key, values) =>
arr.filter(function(n) {
return this.has(n[key]);
}, new Set(values));
const result = filter(arr, key, values);
var arr = [{ "Name": "Alex", "sex":"man","age":50,"workplace":"office"},
{"Name": "Katya", "sex":"woman","age":33,"workplace":"office"},
{"Name": "Igor", "sex":"man","age":22,"workplace":"park"},
{"Name": "Olga", "sex":"woman","age":19,"workplace":"park"},
{"Name": "Maks", "sex":"man","age":40,"workplace":"hotel"}
var arr2 = [];
for(var prop in arr){
if(arr[prop].workplace == "office" || arr[prop].workplace == "hotel"){
arr2.push(arr[prop]);
}
}