Title: Find minimum level in group and combine
Tags: javascript
Question: In my application similar levels are grouped into an array, I need to fetch the minimum level in that group and replace with that minimum level in all the object. I couldn't get the desired result.
let results =  [ [{LEVEL:2,NAME:"ADAMS"},{LEVEL:3,NAME:"JAMES"}],
[{LEVEL:4,NAME:"SHYAM"}],
[{LEVEL:6,NAME:"JIM"},{LEVEL:7,NAME:"ARUN"}]
]
Output
output =  [{LEVEL:2,NAME:"ADAMS"},
{LEVEL:2,NAME:"JAMES"},
{LEVEL:4,NAME:"SHYAM"} ,
{LEVEL:6,NAME:"JIM"},
{LEVEL:6,NAME:"ARUN"}
]
result = results.reduce((r, a) =&gt; {
a.forEach(({LEVEL,NAME}) =&gt; {
var min = r.find(q =&gt; q.LEVEL &gt; LEVEL);
if (!min) r.push({LEVEL,NAME});
return r;
}, []);
console.log(result)

Here is the accepted answer: If you still want to do with reduce and forEach combination then I guess you can do as follows:
const names = [
[ {LEVEL:2,NAME:"ADAMS"}, {LEVEL:3,NAME:"JAMES"} ],
[ {LEVEL:4,NAME:"SHYAM"} ],
[ {LEVEL:6,NAME:"JIM"}, {LEVEL:7,NAME:"ARUN"} ],
const result = names.reduce((accumulator, elem) =&gt; {
elem.forEach(e =&gt; {
const smallestNumber = Math.min.apply( Math, elem.map(a =&gt; a.LEVEL) );
accumulator.push({LEVEL: smallestNumber, NAME: e.NAME});
return accumulator;
}, []);
console.log(result);
I hope this helps!

Here is another answer: For each group, you're going to have to loop through twice. Once to find the min LEVEL, and then again to apply that min. I've used the forEach function here to iterate through these arrays, and I've used a bit of shorthand as well. (group[0] || {LEVEL: 0}) checks if 0 is a valid index of group, and , if not, just uses a default object with a LEVEL of 0 to avoid errors. min &gt; result.LEVEL ? result.LEVEL : min; checks if min is greater than result.LEVEL. If so, it yields result.LEVEL. Otherwise, it yields the current min.
let results = [
[{LEVEL: 2, NAME: "ADAMS"}, {LEVEL: 3, NAME: "JAMES"}],
[{LEVEL: 4, NAME: "SHYAM"}],
[{LEVEL: 6, NAME: "JIM"}, {LEVEL: 7, NAME: "ARUN"}]
results.forEach((group) =&gt; {
var min = (group[0] || {LEVEL: 0}).LEVEL;
group.forEach((result) =&gt; {min = min &gt; result.LEVEL ? result.LEVEL : min;});
group.forEach((result) =&gt; {result.LEVEL = min;});
console.log(results);
Comment for this answer: You're absolutely right. I was in the process of writing the explanation while you commented that. Please check the updated answer.
Comment for this answer: It's always best to explain your answer, not just provide code. Otherwise the OP won't necessarily learn anything.

Here is another answer: Iterate with Array.flatMap(), and map each sub array items into the minimum LEVEL value.
You can use Array.map() to get an arrays of LEVEL, and then spread them in Math.min() to get the minimum value (see the getMinLevel() function).
const results = [[{LEVEL:2,NAME:"ADAMS"},{LEVEL:3,NAME:"JAMES"}],[{LEVEL:4,NAME:"SHYAM"}],[{LEVEL:6,NAME:"JIM"},{LEVEL:7,NAME:"ARUN"}]]

const getMinLevel = arr =&gt; Math.min(...arr.map(o =&gt; o.LEVEL))
const result = results.flatMap(arr =&gt; {
const LEVEL = getMinLevel(arr)
return arr.map(o =&gt; ({
...o,
LEVEL
console.log(result)

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2020:00:01/01/20', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<javascript>', 'Title': 'Find minimum level in group and combine', 'ViewCount': 47, 'id': 59554818, 'user_AboutMe': '', 'user_AccountId': 2620946.0, 'user_DisplayName': 'MKN', 'user_Reputation': 497.0, 'user_Views': 88.0, 'user_id': 2268958.0}}