2

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.

1

3 Answers 3

5

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.