pexichdu
pexichdu

Reputation: 899

Merge object's value into a new array if one of the value match

I have an array of objects that look like this:

const myArray = [
  { taxonomy: 'Orange', slug: 'value1'},
  { taxonomy: 'Orange', slug: 'value2'},
  { taxonomy: 'Green', slug: 'value3'},
]

I want to convert the object's slug into arrays in javascript and map those have the same taxonomy together:

Result:
[
  [value1, value2], // because they share the same taxonomy property.
  [value3]
];

Please help me if you have experienced this. Thank you very much.

Upvotes: 2

Views: 246

Answers (3)

Shilly
Shilly

Reputation: 8589

I use a general reduction function for this kind of things. It takes in 3 parameters. The property you want to examine, if that property will be unique or not, ( unique properties just becomes values, non-unique properties become an array of values ) and finally a transformation function you can use what data you want to extract.

const myArray = [
  { taxonomy: 'Orange', slug: 'value1'},
  { taxonomy: 'Orange', slug: 'value2'},
  { taxonomy: 'Green', slug: 'value3'},
];

const isStr = source => Object.prototype.toString.call( source ) === '[object String]';

const propHash = ( key_reference, is_unique, transformation_value ) => {
    const generateKey = isStr( key_reference )
        ? item => item[ key_reference ]
        : ( item, index, source ) => key_reference( item, index, source );
    return ( hash, item, index, source ) => {
        const key = generateKey( item, index, source );
        if ( !hash.hasOwnProperty( key ) ) {
            if ( is_unique ) hash[ key ] = transformation_value ? transformation_value( item, index, source ) : item;
            else hash[ key ] = [];
        }
        if ( !is_unique ) hash[ key ].push( transformation_value ? transformation_value( item, index, source ): item );
        return hash;
    };
};

const result_hash = myArray.reduce( propHash( 'taxonomy', false, entry => entry.slug ), {});
const result = Object.values( result_hash );

console.log( result );

Upvotes: 0

Mihai Alexandru-Ionut
Mihai Alexandru-Ionut

Reputation: 48357

You could group array's objects by taxonomy property using a hash structure and forEach method.

function groupBy(array, property) {
    var hash = {};
    var result = [];

    array.forEach(function (item) {
        if (!hash[item[property]]) {
            hash[item[property]] = [];
            result.push(hash[item[property]]);
        }
        hash[item[property]].push(item.slug);
    });
    return result;
}
const myArray = [ { taxonomy: 'Orange', slug: 'value1'}, { taxonomy: 'Orange', slug: 'value2'}, { taxonomy: 'Green', slug: 'value3'}, ];

console.log(groupBy(myArray, "taxonomy"));

Upvotes: 1

Nenad Vracar
Nenad Vracar

Reputation: 122027

You could use reduce method with Map and then get the values in an array.

const myArray = [{ taxonomy: 'Orange', slug: 'value1'},{ taxonomy: 'Orange', slug: 'value2'},{ taxonomy: 'Green', slug: 'value3'},]

const res = myArray.reduce((r, {taxonomy, slug}) => {
  return r.set(taxonomy, (r.get(taxonomy) || []).concat(slug))
}, new Map).values()

console.log([...res])

Upvotes: 5

Related Questions