如何在javascript中通过数组获得对象数组



对于数组对象和数组,

仅在

时返回对象数组
  • 相同的值,没有任何模型匹配arrayitem

  • 相同的值和具有相同的模型,无论是否匹配arrayitem

return [] when

  • 相同的值并且在arrayitem
  • 中至少存在一个模型

我尝试

var arrayitem =["swatch", "titan"];
var arrobj1=[
{id:1, name: "xys", model:"car", value: 200},
{id:2, name: "abc", model:"titan", value: 200},
{id:3, name: "tex", model:"plane", value: 300},
]
/*
Expected Output:
value same and model exists in arrays
[]
*/
var arrobj2=[
{id:1, name: "xys", model:"titan", value: 200},
{id:2, name: "abc", model:"plane", value: 300},
{id:3, name: "tes", model:"plane", value: 300},
]
/*
Expected Output:
// both value and model same
[
{id:2, name: "abc", model:"plane", value: 300},
{id:3, name: "tes", model:"plane", value: 300}
]
*/
var arrobj3=[
{id:1, name: "xys", model:"car", value: 200},
{id:2, name: "abc", model:"titan", value: 300},
{id:3, name: "tes", model:"swatch", value: 300},
]
/* Expected Output
//same value and alteast one matches the arrays
[]
*/

const getArr = arrobj => arrobj
.map((obj, i) => {
return arrobj1.find((element, index) => {
if (i !== index && element.value === obj.value) {
if(element.model !== obj.model && 
(!arrayitem.includes(element.model) && !arrayitem.includes(obj.model))){
return obj;
} if(element.model === obj.model) {
return obj;
}
}
})
})
.filter(x => x);
console.log(1, getArr(arrobj1))
console.log(2, getArr(arrobj2))
console.log(3, getArr(arrobj3))

一种方法是将代码分解为单个任务/函数,这些任务/函数可以组合在一起,以覆盖OP的场景/请求。

因此,可以编写两个可配置的过滤函数,其中可以传递property-name/key,以便将正确的属性值与另外传递的值列表中的值进行比较。

  • 一个函数返回与值列表匹配的所有项。
  • 另一个返回所有不匹配值列表的项。

为了降低成本(嵌套迭代),可以实现一个辅助函数,它从值列表创建一个映射(单个迭代),该映射被两个过滤函数用于廉价的值查找。

还剩下一项任务,即创建相同项的列表,其中的一致性由一组指定键的每个属性名称/key的属性值相等来保证/定义。

因此,我们将再次编写一个可配置函数,其中除了项列表外,还将key配置传递给。

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; }

相关内容

  • 没有找到相关文章

最新更新