1

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.

4
  • Can you just add ...details[index].assignment_info.info, as the 3rd line of the object in the first function? Commented Sep 3, 2019 at 7:02
  • you can pass third argument stating you want 'getDetailsBuyersWithJobIds' or 'getDetailsAssignmentInfosWithJobIds' and just do condition there Commented Sep 3, 2019 at 7:06
  • @CertainPerformance I can, but the problem is that at the moment I'm taking two fields from the object, there will be more in the future Commented Sep 3, 2019 at 7:06
  • I guess you could pass getDetailsAssignmentInfosWithJobIds the callback used with .map (though that also makes getDetailsAssignmentInfosWithJobIds itself nearly useless) Commented Sep 3, 2019 at 7:08

2 Answers 2

2

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

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

1 Comment

I think this is better than rewriting a function
1

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.

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.