function getValueMapFromList(list) {
return new Map(list.map(value => [value, value]));
}
function getPropertyValueMatchingItemsByKey(
targetList,
valueList,
key,
) {
const lookup = getValueMapFromList(valueList);
return targetList.filter(item => lookup.has(item[key]));
}
function getPropertyValueNonMatchingItemsByKey(
targetList,
valueList,
key,
) {
const lookup = getValueMapFromList(valueList);
return targetList.filter(item => !lookup.has(item[key]));
}
function getItemsOfSamePropertyValuesByKeys(
targetList = [],
keyConfig = {},
) {
let { primaryKey = null, keyList = [] } = keyConfig;
if (Array.isArray(keyConfig)) {
keyList = keyConfig;
} else {
keyList.unshift(primaryKey);
}
let sameValueItems = targetList
.filter((targetItem, idx, arr) => [
...arr.slice(0, idx),
...arr.slice(idx + 1),
]
.some(siblingItem => keyList
.every(key =>
siblingItem[key] === targetItem[key]
)
)
);
if (primaryKey && (typeof primaryKey === 'string')) {
// an available primary key triggers the grouping
// of sub item-lists by this key which results in
// an array of grouped arrays (the very sub items).
sameValueItems = Object.values(
sameValueItems.reduce((group, item) => {
(group[item[primaryKey]] ??= []).push(item)
return group;
}, Object.create(null))
);
}
return sameValueItems;
}
const listOfPropertyValues = ["swatch", "titan"];
const itemList_1 = [
{ id: 1, name: "xys", model: "car", value: 200 },
{ id: 2, name: "abc", model: "titan", value: 200 },
{ id: 3, name: "tex", model: "plane", value: 300 },
];
const itemList_2 = [
{ id:1, name: "xys", model: "titan", value: 200 },
{ id:2, name: "abc", model: "plane", value: 300 },
{ id:3, name: "tes", model: "plane", value: 300 },
];
const itemList_3 = [
{ id:1, name: "xys", model: "car", value: 200 },
{ id:2, name: "abc", model: "titan", value: 300 },
{ id:3, name: "tes", model: "swatch", value: 300 },
{ id:4, name: "xys", model: "car", value: 200 },
{ id:5, name: "abc", model: "titan", value: 300 },
];
// ... return the array of objects only when ...
console.log("same values from models which do not match `arrayitem`");
console.log('...arrayitem...', listOfPropertyValues);
console.log(
'...for `itemList_1`...',
getItemsOfSamePropertyValuesByKeys(
// items of non matching `model` values.
getPropertyValueNonMatchingItemsByKey(
itemList_1, listOfPropertyValues, 'model'
),
// the key configuration
// - either a real config which returns a grouped array of arrays.
// { primaryKey: 'model', keyList: ['value'] }
// - or a simple key list which returns a flat ungrouped array.
['model', 'value']
)
);
console.log(
'...for `itemList_2`...',
getItemsOfSamePropertyValuesByKeys(
// items of non matching `model` values.
getPropertyValueNonMatchingItemsByKey(
itemList_2, listOfPropertyValues, 'model'
),
['model', 'value'] // { primaryKey: 'model', keyList: ['value'] }
)
);
console.log(
'...for `itemList_3`...',
getItemsOfSamePropertyValuesByKeys(
// items of non matching `model` values.
getPropertyValueNonMatchingItemsByKey(
itemList_3, listOfPropertyValues, 'model'
),
['model', 'value'] // { primaryKey: 'model', keyList: ['value'] }
)
);
console.log("same values from models regardless of matching `arrayitem`");
console.log('...arrayitem...', listOfPropertyValues);
console.log(
'...for `itemList_1`...',
getItemsOfSamePropertyValuesByKeys(
itemList_1,
{ primaryKey: 'model', keyList: ['value'] } // ['model', 'value']
)
);
console.log(
'...for `itemList_2`...',
getItemsOfSamePropertyValuesByKeys(
itemList_2,
{ primaryKey: 'model', keyList: ['value'] } // ['model', 'value']
)
);
console.log(
'...for `itemList_3`...',
getItemsOfSamePropertyValuesByKeys(
itemList_3,
{ primaryKey: 'model', keyList: ['value'] } // ['model', 'value']
)
);
console.log("same values from models which match `arrayitem`");
console.log('...arrayitem...', listOfPropertyValues);
console.log(
'...for `itemList_1`...',
getItemsOfSamePropertyValuesByKeys(
// items of matching `model` values.
getPropertyValueMatchingItemsByKey(
itemList_1, listOfPropertyValues, 'model'
),
{ primaryKey: 'model', keyList: ['value'] } // ['model', 'value']
)
);
console.log(
'...for `itemList_2`...',
getItemsOfSamePropertyValuesByKeys(
// items of matching `model` values.
getPropertyValueMatchingItemsByKey(
itemList_2, listOfPropertyValues, 'model'
),
{ primaryKey: 'model', keyList: ['value'] } // ['model', 'value']
)
);
console.log(
'...for `itemList_3`...',
getItemsOfSamePropertyValuesByKeys(
// items of matching `model` values.
getPropertyValueMatchingItemsByKey(
itemList_3, listOfPropertyValues, 'model'
),
{ primaryKey: 'model', keyList: ['value'] } // ['model', 'value']
)
);
.as-console-wrapper { min-height: 100%!important; top: 0; }