简体   繁体   中英

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. Any better solutions?

You can iterate over the list, and every time, reduce the object properties:

 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.

 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. The new objects are created using Object.fromEntries() , Object.entries() , .filter() and .map() . You can use these methods as follows:

  1. Use Object.entries() to obtain an array of [[key, value],...] pairs from your object.

  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 "_"

  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. To do this, you can split your key using "_" to form an array of two parts - the "param" & the alpha country code. You can use .pop() to obtain the last string in your array of parts (ie: the alpha country code)

  4. Wrap this filtered/mapped array of key-value pairs in a call to 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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