简体   繁体   English

从 object 数组返回所有键:值对,其中键在 javascript 中包含下划线

[英]Returning all key:value pairs from object array that key includes underscore in javascript

I have rows that contain data in form like this:我的行包含以下形式的数据:

{
 id:"id",
 param:"street1",
 param:"street2",
 param_eu:"street",
 param_ru:"street",
},
{
 id:"id2",
 param:"street1",
 param:"street2",
 param_fr:"street",
 param_cz:"street",
},

I would like to pick only pairs that contain "_" and only keep the country as key name, like this:我只想选择包含“_”的对,并且只保留国家作为键名,如下所示:

{ 
 eu:"street",
 ru:"street"
},
{ 
 fr:"street",
 cz:"street"
},

Managed to get it done with multiple inner for loops but that is huge.设法通过多个内部 for 循环来完成它,但这是巨大的。 Any better solutions?有更好的解决方案吗?

You can iterate over the list, and every time, reduce the object properties:您可以遍历列表,并且每次都减少 object 属性:

 const data = [ { id:"id", param:"street1", param:"street2", param_eu:"street", param_ru:"street" }, { id:"id2", param:"street1", param:"street2", param_fr:"street", param_cz:"street" } ]; // iterate over objects const res = data.reduce((countries, item) => { // reduce object properties const itemWithCountries = Object.entries(item).reduce((acc, [key, value]) => { if(key.includes('_')) acc[key.split('_').pop()] = value; return acc; }, {}); // push reduced object if(Object.keys(itemWithCountries).length > 0) countries.push(itemWithCountries); return countries; }, []); console.log(res);

You could map new object by getting all entries from the objects, get the one you want and build object from the entries as new objects.您可以 map new object 通过从对象中获取所有条目,获取所需的条目并从条目中构建 object 作为新对象。

 const array = [{ id: "id", param: "street1", param: "street2", param_eu: "street", param_ru: "street" }, { id: "id2", param: "street1", param: "street2", param_fr: "street", param_cz: "street" }], result = array.map(object => Object.fromEntries(Object.entries(object).filter(([key]) => key.includes('_')) )); console.log(result);

You can first use .map() on your array to map each object to a new object.您可以首先在您的阵列上使用.map()到 map 每个 object 到一个新的 object。 The new objects are created using Object.fromEntries() , Object.entries() , .filter() and .map() .新对象是使用Object.fromEntries()Object.entries().filter().map()创建的。 You can use these methods as follows:您可以按如下方式使用这些方法:

  1. Use Object.entries() to obtain an array of [[key, value],...] pairs from your object.使用Object.entries()从 object 中获取[[key, value],...]对的数组。

  2. On the above key-value pair array, use .filter() to obtain only the keys that contain an underscore in them by checking if the key includes an underscore "_"在上面的键值对数组上,使用.filter()通过检查key是否包含下划线"_"来仅获取其中包含下划线的键

  3. Once you have filtered your array, use .map() on your filtered key-value array to map each inner array (ie: [key, value] ) to a new array, where everything to the left of the underscore is removed.过滤数组后,在过滤后的键值数组上使用.map()到 map 每个内部数组(即: [key, value] )到一个新数组,其中下划线左侧的所有内容都被删除。 To do this, you can split your key using "_" to form an array of two parts - the "param" & the alpha country code.为此,您可以使用"_" 拆分您的密钥以形成一个由两部分组成的数组 - "param"和字母国家代码。 You can use .pop() to obtain the last string in your array of parts (ie: the alpha country code)您可以使用.pop()获取零件数组中的最后一个字符串(即:字母国家/地区代码)

  4. Wrap this filtered/mapped array of key-value pairs in a call to Object.fromEntries() .在对Object.fromEntries()的调用中包装这个过滤/映射的键值对数组。 Since this takes an array of [[key, value], ...] pairs, it will be able to build an object for you, where each inner [key, value] array is converted into a {key: value, ...} in the resulting object.由于这需要一个由[[key, value], ...]对组成的数组,因此它将能够为您构建一个 object,其中每个内部[key, value]数组都转换为一个{key: value, ...}在生成的 object 中。

See example below:请参见下面的示例:

 const arr = [{ id: "id", param: "street1", param: "street2", param_eu: "street", param_ru: "street", }, { id: "id2", param: "street1", param: "street2", param_fr: "street", param_cz: "street", } ]; const res = arr.map( obj => Object.fromEntries( Object.entries(obj).filter( ([key]) => key.includes("_") ).map(([key, val])=> [key.split("_").pop(), val]) ) ); console.log(res);

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

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