MegaRoks
MegaRoks

Reputation: 948

Combining two functions for working with an array of objects

I have two arrays of objects.

const details = [
    {
        ciphertext: 1234,
        buyer: {
            op_timezone: 7689,
            op_city: 'Name1',
        },
        assignment_info: { 
            info: {
                end_data: 1456,
                start_date: 2389,
            }
        }
    },
    {
        ciphertext: 5678,
        buyer: {
            op_timezone: 4568,
            op_city: 'Name2',
        },
        assignment_info: { 
            info: {
                end_data: 3467,
                start_date: 8753,
            }
        }
    },
];

const jobIds = [
    {
        id: 1,
    },
    {
        id: 2,
    },
];

I need to combine two arrays and take the assignment_info.info and buyer fields from each object.

function getDetailsBuyersWithJobIds(jobIds, details) {
    return jobIds.map((item, index) => ({
        ...item,
        ...details[index].buyer,
    }));
};

function getDetailsAssignmentInfosWithJobIds(jobIds, details) {
    return jobIds.map((item, index) => ({
        ...item,
        ...details[index].assignment_info.info,
    }));
};

The question is, how can two functions be combined into one?

That there would be no duplicate function, since they perform the same thing.

Upvotes: 1

Views: 667

Answers (2)

sjahan
sjahan

Reputation: 5960

You can do a generic mapping function and pass it a getter function that will be able to fetch the proper data, not sure it will help the global readibility though.

What do you think about that?

const genericMapper = (getter) => (item, index) => ({
    ...item,
    ...getter(details[index]),
});

function getDetailsBuyersWithJobIds(jobIds, details) {
    return jobIds.map(genericMapper(it => it.buyer));
};

function getDetailsAssignmentInfosWithJobIds(jobIds, details) {
    return jobIds.map(genericMapper(it => it.assignment_info.info));
};

const details = [
    {
        ciphertext: 1234,
        buyer: {
            op_timezone: 7689,
            op_city: 'Name1',
        },
        assignment_info: { 
            info: {
                end_data: 1456,
                start_date: 2389,
            }
        }
    },
    {
        ciphertext: 5678,
        buyer: {
            op_timezone: 4568,
            op_city: 'Name2',
        },
        assignment_info: { 
            info: {
                end_data: 3467,
                start_date: 8753,
            }
        }
    },
];

const jobIds = [
    {
        id: 1,
    },
    {
        id: 2,
    },
];

console.log(getDetailsBuyersWithJobIds(jobIds, details));
console.log(getDetailsAssignmentInfosWithJobIds(jobIds, details));

Upvotes: 2

Code Maniac
Code Maniac

Reputation: 37755

You can add values on return object based on condition something like this

const details = [{ciphertext: 1234,buyer: {op_timezone: 7689,op_city: 'Name1',},assignment_info: {info: {end_data: 1456,start_date: 2389,}}},{ciphertext: 5678,buyer: {op_timezone: 4568,op_city: 'Name2',},assignment_info: {info: {end_data: 3467,start_date: 8753,}}},];

const jobIds = [{id: 1,},{id: 2,},];

function getDetails(jobIds, details, props = {
  getBuyer: true
}) {
  return jobIds.map((item, index) => ({
    ...item,
    ...(props.getBuyer && { ...details[index].buyer
    }),
    ...(props.getAssignment && { ...details[index].assignment_info.info
    })
  }));
};


console.log(getDetails([1], details, {
  getBuyer: true
}))
console.log(getDetails([1], details, {
  getAssignment: true
}))

Here props = { getBuyer: true} used to set a default value.

Upvotes: 1

Related Questions