-2

I need Grouping two arrays, case second array no have code then set ''(other questions and answers, do not show how to do it that way).

FIRST ARRAY:

[ { code: "1", description: "one", activity: "5" }, { code: "2", description: "two", activity: "30" }, { code: "3", description: "tree", activity: "898499949" }, { code: "4", description: "four", activity: "65465" }, { code: "5", description: "five", activity: "123" }, { code: "6", description: "six", activity: "111" }, ] 

SECOND ARRAY:

[ { code: "1", value: 500 }, { code: "1", value: 300 }, { code: "2", value: 20 }, { code: "3", value: 1950 }, { code: "6", value: 69990 }, { code: "6", value: 2330 }, { code: "6", value: 6120 }, { code: "6", value: 2 }, ] 

I need set value in first ARRAY, if code equal in first and second ARRAY, case second array no have code then set '':

[ { code: "1", description: "one", activity: "5", value: 500 }, { code: "2", description: "two", activity: "30", value: 20 }, { code: "3", description: "tree", activity: "898499949", value: 1950 }, { code: "4", description: "four", activity: "65465", value: "" }, { code: "5", description: "five", activity: "123", value: "" }, { code: "6", description: "six", activity: "111", value: "69990" }, ] 

Expected new result:

[ { code: "1", description: "one", activity: "5", value: 500 }, { code: "1", description: "one", activity: "5", value: 300}, { code: "2", description: "two", activity: "30", value: 20 }, { code: "3", description: "tree", activity: "898499949", value: 1950 }, { code: "6", description: "six", activity: "111", value: "69990" }, { code: "6", description: "six", activity: "111", value: "2330 " }, { code: "6", description: "six", activity: "111", value: "6120 " }, { code: "6", description: "six", activity: "111", value: "2" }, ] 
3
  • 4
    Does this answer your question? JavaScript merging objects by id Commented Jul 9, 2020 at 17:04
  • @SebastiánPuchet no, I need to insert an empty value in "value", in case there is no value in the second array. Commented Jul 9, 2020 at 17:06
  • 1
    Come on bro, you just have to modify the algorithm... _.map(a1, function(item){ return _.extend(item, _.find(a2, { code: item.code }) || { value: '' }); }); Commented Jul 9, 2020 at 17:25

1 Answer 1

1

You could take a Map for the second array and map the first array and spread the mapped object or a default value.

const first = [{ code: '1', description: 'one', activity: '5' }, { code: '2', description: 'two', activity: '30' }, { code: '3', description: 'tree', activity: '898499949' }, { code: '4', description: 'four', activity: '65465' }, { code: '5', description: 'five', activity: '123' }, { code: '6', description: 'six', activity: '111' }], second = [{ code: '1', value: 500 }, { code: '2', value: 20 }, { code: '3', value: 1950 }, { code: '6', value: 69990 }], key = 'code', result = first.map( (m => o => ({ ...o, ...(m.get(o[key]) || { value: '' }) })) (new Map(second.map(o => [o[key], o]))) ); console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Second.

const first = [{ code: "1", description: "one", activity: "5" }, { code: "2", description: "two", activity: "30" }, { code: "3", description: "tree", activity: "898499949" }, { code: "4", description: "four", activity: "65465" }, { code: "5", description: "five", activity: "123" }, { code: "6", description: "six", activity: "111" }], second = [{ code: "1", value: 500 }, { code: "1", value: 300 }, { code: "2", value: 20 }, { code: "3", value: 1950 }, { code: "6", value: 69990 }, { code: "6", value: 2330 }, { code: "6", value: 6120 }, { code: "6", value: 2 }], key = 'code', result = second.map( (m => o => ({ ...m.get(o[key]), ...o })) (new Map(first.map(o => [o[key], o]))) ); console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

11 Comments

if the value is empty, how can I not import this line into array 3?
do you have an example? maybe you could ask a new question?
I updated question in "Expected new result:", if array two does not have a "code" then these lines without the code should not appear.
if I create another question, they will deny me again ...
it looks like you reversed the order.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.