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" }, ]
_.map(a1, function(item){ return _.extend(item, _.find(a2, { code: item.code }) || { value: '' }); });