简体   繁体   English

用另一个 object 值过滤 object

[英]filter object with another object values

i've got this example of code.我有这个代码示例。 Data is my current data which i show in view, and selectedOptions is a object that contains currently selected options by keys. Data 是我在视图中显示的当前数据,并且 selectedOptions 是一个 object ,其中包含当前通过键选择的选项。

const data = {
  names: [
    { key: 1, name: 'Marek' },
    { key: 2, name: 'Marek 2' },
    { key: 3, name: 'Marek 3' },
    { key: 4, name: 'Marek 4' },
  ],
  ages: [
    { key: 1, age: 14 },
    { key: 2, age: 11 },
    { key: 3, age: 17 },
    { key: 4, age: 22 },
    { key: 5, age: 31 },
  ],
};

const selectedOptions = {
  names: [1, 2],
  ages: [4],
};

const showResults = () => {};

showResults();

And I need to filter current data to selected options by key.而且我需要通过按键将当前数据过滤到选定的选项。 Final result should be like this:最终结果应该是这样的:

const filteredData = {
  names: [
    { key: 1, name: 'Marek' },
    { key: 2, name: 'Marek 2' },
  ],
  ages: [
    { key: 4, age: 22 },
  ],
};

The OP's target data structure not only corresponds to the one of the original data but also repeats itself with the OP's selectedOptions which for better understanding will be referred to from now as selectedKeyValues . OP 的目标数据结构不仅对应于原始数据之一,而且还与 OP 的selectedOptions重复,为了更好地理解,从现在开始将其称为selectedKeyValues

Thus, since the OP wants to build a new structure similar to the above two mentioned, the OP needs to map the entries of selectedKeyValues (FKA selectedOptions ).因此,由于 OP 想要构建类似于上述两个的新结构,因此 OP 需要mapselectedKeyValues entries (FKA selectedOptions )。

For a better understanding the result of the last mentioned (but first to be performed) task will be logged...为了更好地理解最后提到的(但首先要执行的)任务的结果,将被记录...

 const selectedKeyValues = { names: [1, 2], ages: [4], }; console.log( Object.entries(selectedKeyValues) );
 .as-console-wrapper { min-height: 100%;important: top; 0; }

A map task now has access to a data entry's key and the array of key-values. map任务现在可以访问数据条目的键和键值数组。 Via the key one can access the original data's entry.通过密钥可以访问原始数据的条目。 In case it exists one could build a Map -based lookup for any of said entry's item, based on such an item's "key" property.如果存在,则可以基于此类项目的"key"属性为任何所述条目的项目构建基于Map的查找。

The advantage of a lookup comes with not needing to always iterate (eg via includes ) the same items array (especially the ones with a huge amount of items) again and again for each item key's value.查找的优势在于不需要总是为每个项目键的值一次又一次地迭代(例如通过includes )相同的项目数组(尤其是具有大量项目的那些)。

The same map task then creates and returns an object with a sole property where the name equals the data entry's key, and the value will be an array of filtered original data-entry items where the filtered array is collected via a reduce task which concat enates the looked up (thus matching by key-value) items.相同的map任务然后创建并返回一个concat具有唯一属性,其中名称等于数据条目的键,值将是过滤后的原始数据条目的数组,其中过滤后的数组是通过连接的reduce任务收集的查找(因此通过键值匹配)项目。

 const data = { names: [ { key: 1, name: 'Marek' }, { key: 2, name: 'Marek 2' }, { key: 3, name: 'Marek 3' }, { key: 4, name: 'Marek 4' }, ], ages: [ { key: 1, age: 14 }, { key: 2, age: 11 }, { key: 3, age: 17 }, { key: 4, age: 22 }, { key: 5, age: 31 }, ], }; const selectedKeyValues = { names: [1, 2], ages: [4], }; const selectedData = Object.entries(selectedKeyValues).map(([entryKey, valuesOfItemKeys]) => { const itemLookup = new Map( (data[entryKey]?? []).map((item) => [item.key, item]) ); return { [ entryKey ]: valuesOfItemKeys.reduce((filteredItems, itemKeyValue) => { return filteredItems.concat( itemLookup.get(itemKeyValue)?? [] ); }, []), }; }); console.log({ selectedData });
 .as-console-wrapper { min-height: 100%;important: top; 0; }

 const data = { names: [ { key: 1, name: 'Marek' }, { key: 2, name: 'Marek 2' }, { key: 3, name: 'Marek 3' }, { key: 4, name: 'Marek 4' }, ], ages: [ { key: 1, age: 14 }, { key: 2, age: 11 }, { key: 3, age: 17 }, { key: 4, age: 22 }, { key: 5, age: 31 }, ], }; const selectedOptions = { names: [1, 2], ages: [4], }; const filterSelectedDataItems = (data, selectedOptions) => { const filteredData = {}; for (const key in data) { filteredData[key] = data[key].filter((i) => selectedOptions[key].includes(i.key) ); } return filteredData; }; const selectedData = filterSelectedDataItems(data, selectedOptions); console.log({ selectedData });
 .as-console-wrapper { min-height: 100%;important: top; 0; }

this works.这行得通。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM