Grouping elements in array by multiple properties underscore









up vote
0
down vote

favorite
1












During work, I was given this task : to group elements with similar properties in the array.



In general, the problem is as follows :



var order = [

"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Speed"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Discharge pressure"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Speed"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"RCU",
"equipment":"37 P 552 B",
"parameter":"Discharge pressure"

]


If I group this elements by 'ru', 'area', 'unit', 'tot', 'equipment' and 'parameter', I will get this result :



var result = [

"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"tot":2,
"equipment":"37 P 552 A",
"parameter":"Speed"
,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"tot":1,
"equipment":"37 P 552 A",
"parameter":"Discharge Pressure"
,
"ru":"R401",
"area":"RFCC",
"unit":"RCU",
"tot":1,
"equipment":"37 P 552 B",
"parameter":"Discharge Pressure"
];


After some experimentation, I came to the following code :



 var groups = _.groupBy(order, function(value) 
return value.ru + "#" + value.area + "#" + value.unit + "#" + value.equipment + "#" + value.parameter + "#";
);

groups = _.map(groups, function(group)
return _.extend(group[0], tot: group.length);
);


Now I have problem how to group the similar result elements. If there is someone around here, please help me. Thanks in advance.










share|improve this question























  • What do you mean by similar result elements?
    – Andreas
    Nov 9 at 3:59










  • @Andreas how to group the result data above, because the result have some similar elements.
    – Hamzah Aznageel
    Nov 9 at 4:01










  • Then, what is the problem with your current code?
    – Andreas
    Nov 9 at 4:05










  • This solution works, but is this a right and best way? It still looks a little ugly to me.
    – Hamzah Aznageel
    Nov 9 at 4:11






  • 1




    Please edit your question accordingly, because currently you are saying that you have a problem with the grouping, not with the optimizing
    – Andreas
    Nov 9 at 4:15














up vote
0
down vote

favorite
1












During work, I was given this task : to group elements with similar properties in the array.



In general, the problem is as follows :



var order = [

"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Speed"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Discharge pressure"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Speed"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"RCU",
"equipment":"37 P 552 B",
"parameter":"Discharge pressure"

]


If I group this elements by 'ru', 'area', 'unit', 'tot', 'equipment' and 'parameter', I will get this result :



var result = [

"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"tot":2,
"equipment":"37 P 552 A",
"parameter":"Speed"
,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"tot":1,
"equipment":"37 P 552 A",
"parameter":"Discharge Pressure"
,
"ru":"R401",
"area":"RFCC",
"unit":"RCU",
"tot":1,
"equipment":"37 P 552 B",
"parameter":"Discharge Pressure"
];


After some experimentation, I came to the following code :



 var groups = _.groupBy(order, function(value) 
return value.ru + "#" + value.area + "#" + value.unit + "#" + value.equipment + "#" + value.parameter + "#";
);

groups = _.map(groups, function(group)
return _.extend(group[0], tot: group.length);
);


Now I have problem how to group the similar result elements. If there is someone around here, please help me. Thanks in advance.










share|improve this question























  • What do you mean by similar result elements?
    – Andreas
    Nov 9 at 3:59










  • @Andreas how to group the result data above, because the result have some similar elements.
    – Hamzah Aznageel
    Nov 9 at 4:01










  • Then, what is the problem with your current code?
    – Andreas
    Nov 9 at 4:05










  • This solution works, but is this a right and best way? It still looks a little ugly to me.
    – Hamzah Aznageel
    Nov 9 at 4:11






  • 1




    Please edit your question accordingly, because currently you are saying that you have a problem with the grouping, not with the optimizing
    – Andreas
    Nov 9 at 4:15












up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





During work, I was given this task : to group elements with similar properties in the array.



In general, the problem is as follows :



var order = [

"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Speed"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Discharge pressure"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Speed"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"RCU",
"equipment":"37 P 552 B",
"parameter":"Discharge pressure"

]


If I group this elements by 'ru', 'area', 'unit', 'tot', 'equipment' and 'parameter', I will get this result :



var result = [

"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"tot":2,
"equipment":"37 P 552 A",
"parameter":"Speed"
,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"tot":1,
"equipment":"37 P 552 A",
"parameter":"Discharge Pressure"
,
"ru":"R401",
"area":"RFCC",
"unit":"RCU",
"tot":1,
"equipment":"37 P 552 B",
"parameter":"Discharge Pressure"
];


After some experimentation, I came to the following code :



 var groups = _.groupBy(order, function(value) 
return value.ru + "#" + value.area + "#" + value.unit + "#" + value.equipment + "#" + value.parameter + "#";
);

groups = _.map(groups, function(group)
return _.extend(group[0], tot: group.length);
);


Now I have problem how to group the similar result elements. If there is someone around here, please help me. Thanks in advance.










share|improve this question















During work, I was given this task : to group elements with similar properties in the array.



In general, the problem is as follows :



var order = [

"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Speed"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Discharge pressure"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Speed"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"RCU",
"equipment":"37 P 552 B",
"parameter":"Discharge pressure"

]


If I group this elements by 'ru', 'area', 'unit', 'tot', 'equipment' and 'parameter', I will get this result :



var result = [

"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"tot":2,
"equipment":"37 P 552 A",
"parameter":"Speed"
,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"tot":1,
"equipment":"37 P 552 A",
"parameter":"Discharge Pressure"
,
"ru":"R401",
"area":"RFCC",
"unit":"RCU",
"tot":1,
"equipment":"37 P 552 B",
"parameter":"Discharge Pressure"
];


After some experimentation, I came to the following code :



 var groups = _.groupBy(order, function(value) 
return value.ru + "#" + value.area + "#" + value.unit + "#" + value.equipment + "#" + value.parameter + "#";
);

groups = _.map(groups, function(group)
return _.extend(group[0], tot: group.length);
);


Now I have problem how to group the similar result elements. If there is someone around here, please help me. Thanks in advance.







javascript arrays json underscore.js






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 4:01

























asked Nov 9 at 3:53









Hamzah Aznageel

198




198











  • What do you mean by similar result elements?
    – Andreas
    Nov 9 at 3:59










  • @Andreas how to group the result data above, because the result have some similar elements.
    – Hamzah Aznageel
    Nov 9 at 4:01










  • Then, what is the problem with your current code?
    – Andreas
    Nov 9 at 4:05










  • This solution works, but is this a right and best way? It still looks a little ugly to me.
    – Hamzah Aznageel
    Nov 9 at 4:11






  • 1




    Please edit your question accordingly, because currently you are saying that you have a problem with the grouping, not with the optimizing
    – Andreas
    Nov 9 at 4:15
















  • What do you mean by similar result elements?
    – Andreas
    Nov 9 at 3:59










  • @Andreas how to group the result data above, because the result have some similar elements.
    – Hamzah Aznageel
    Nov 9 at 4:01










  • Then, what is the problem with your current code?
    – Andreas
    Nov 9 at 4:05










  • This solution works, but is this a right and best way? It still looks a little ugly to me.
    – Hamzah Aznageel
    Nov 9 at 4:11






  • 1




    Please edit your question accordingly, because currently you are saying that you have a problem with the grouping, not with the optimizing
    – Andreas
    Nov 9 at 4:15















What do you mean by similar result elements?
– Andreas
Nov 9 at 3:59




What do you mean by similar result elements?
– Andreas
Nov 9 at 3:59












@Andreas how to group the result data above, because the result have some similar elements.
– Hamzah Aznageel
Nov 9 at 4:01




@Andreas how to group the result data above, because the result have some similar elements.
– Hamzah Aznageel
Nov 9 at 4:01












Then, what is the problem with your current code?
– Andreas
Nov 9 at 4:05




Then, what is the problem with your current code?
– Andreas
Nov 9 at 4:05












This solution works, but is this a right and best way? It still looks a little ugly to me.
– Hamzah Aznageel
Nov 9 at 4:11




This solution works, but is this a right and best way? It still looks a little ugly to me.
– Hamzah Aznageel
Nov 9 at 4:11




1




1




Please edit your question accordingly, because currently you are saying that you have a problem with the grouping, not with the optimizing
– Andreas
Nov 9 at 4:15




Please edit your question accordingly, because currently you are saying that you have a problem with the grouping, not with the optimizing
– Andreas
Nov 9 at 4:15












2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










How about this:






var order = [

"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Speed"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Discharge pressure"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Speed"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"RCU",
"equipment":"37 P 552 B",
"parameter":"Discharge pressure"

];
var result = ;
order.forEach(current=>
let index = -1;
result.forEach((c,i)=>
if (c.ru==current.ru && c.area==current.area && c.unit==current.unit && c.tot==current.tot && c.equipment==current.equipment && c.parameter==current.parameter)
index = i;

);
// console.log(index);
if(index==-1)
result.push(current);
else
result[index]["tot"] = result[index]["tot"]+current["tot"];

);
console.log(result);








share|improve this answer




















  • how to get specific object for example 'ru'?
    – Hamzah Aznageel
    Nov 9 at 4:41










  • Get result just has only "ru"?
    – protoproto
    Nov 9 at 4:44










  • yes, get result only "ru", I think just console.log(result.ru), but the result is undefined
    – Hamzah Aznageel
    Nov 9 at 4:55










  • You can use: console.log(order.map(current=>current["ru"])); --> get only column 'ru'!
    – protoproto
    Nov 9 at 5:03

















up vote
0
down vote













Maybe you can avoid grouping and directly update the tot property of the merged object in your "index" map/object if you encounter similar elements:



Here is a solution that does that with reduce:






const order = [

"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Speed"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Discharge pressure"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Speed"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"RCU",
"equipment":"37 P 552 B",
"parameter":"Discharge pressure"

]

const merged = Object.values(order.reduce((acc, curr) =>
const key = `$curr.ru#$curr.area#$curr.unit#$curr.equipment#$curr.parameter`;
if (key in acc) acc[key].tot += 1;
else acc[key] = Object.assign(, curr);
return acc;
, ));

console.log(merged);
console.log(merged.map(d => d.ru));








share|improve this answer






















  • how to get all "ru" objects in array?
    – Hamzah Aznageel
    Nov 9 at 5:01










  • @HamzahAznageel it's not that complicated. As @protoproto said, you can use map (see updated answer). merged.map(d => d.ru)
    – slider
    Nov 9 at 5:08










  • well, okay thank you so much
    – Hamzah Aznageel
    Nov 9 at 6:11










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53219628%2fgrouping-elements-in-array-by-multiple-properties-underscore%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










How about this:






var order = [

"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Speed"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Discharge pressure"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Speed"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"RCU",
"equipment":"37 P 552 B",
"parameter":"Discharge pressure"

];
var result = ;
order.forEach(current=>
let index = -1;
result.forEach((c,i)=>
if (c.ru==current.ru && c.area==current.area && c.unit==current.unit && c.tot==current.tot && c.equipment==current.equipment && c.parameter==current.parameter)
index = i;

);
// console.log(index);
if(index==-1)
result.push(current);
else
result[index]["tot"] = result[index]["tot"]+current["tot"];

);
console.log(result);








share|improve this answer




















  • how to get specific object for example 'ru'?
    – Hamzah Aznageel
    Nov 9 at 4:41










  • Get result just has only "ru"?
    – protoproto
    Nov 9 at 4:44










  • yes, get result only "ru", I think just console.log(result.ru), but the result is undefined
    – Hamzah Aznageel
    Nov 9 at 4:55










  • You can use: console.log(order.map(current=>current["ru"])); --> get only column 'ru'!
    – protoproto
    Nov 9 at 5:03














up vote
0
down vote



accepted










How about this:






var order = [

"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Speed"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Discharge pressure"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Speed"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"RCU",
"equipment":"37 P 552 B",
"parameter":"Discharge pressure"

];
var result = ;
order.forEach(current=>
let index = -1;
result.forEach((c,i)=>
if (c.ru==current.ru && c.area==current.area && c.unit==current.unit && c.tot==current.tot && c.equipment==current.equipment && c.parameter==current.parameter)
index = i;

);
// console.log(index);
if(index==-1)
result.push(current);
else
result[index]["tot"] = result[index]["tot"]+current["tot"];

);
console.log(result);








share|improve this answer




















  • how to get specific object for example 'ru'?
    – Hamzah Aznageel
    Nov 9 at 4:41










  • Get result just has only "ru"?
    – protoproto
    Nov 9 at 4:44










  • yes, get result only "ru", I think just console.log(result.ru), but the result is undefined
    – Hamzah Aznageel
    Nov 9 at 4:55










  • You can use: console.log(order.map(current=>current["ru"])); --> get only column 'ru'!
    – protoproto
    Nov 9 at 5:03












up vote
0
down vote



accepted







up vote
0
down vote



accepted






How about this:






var order = [

"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Speed"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Discharge pressure"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Speed"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"RCU",
"equipment":"37 P 552 B",
"parameter":"Discharge pressure"

];
var result = ;
order.forEach(current=>
let index = -1;
result.forEach((c,i)=>
if (c.ru==current.ru && c.area==current.area && c.unit==current.unit && c.tot==current.tot && c.equipment==current.equipment && c.parameter==current.parameter)
index = i;

);
// console.log(index);
if(index==-1)
result.push(current);
else
result[index]["tot"] = result[index]["tot"]+current["tot"];

);
console.log(result);








share|improve this answer












How about this:






var order = [

"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Speed"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Discharge pressure"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Speed"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"RCU",
"equipment":"37 P 552 B",
"parameter":"Discharge pressure"

];
var result = ;
order.forEach(current=>
let index = -1;
result.forEach((c,i)=>
if (c.ru==current.ru && c.area==current.area && c.unit==current.unit && c.tot==current.tot && c.equipment==current.equipment && c.parameter==current.parameter)
index = i;

);
// console.log(index);
if(index==-1)
result.push(current);
else
result[index]["tot"] = result[index]["tot"]+current["tot"];

);
console.log(result);








var order = [

"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Speed"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Discharge pressure"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Speed"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"RCU",
"equipment":"37 P 552 B",
"parameter":"Discharge pressure"

];
var result = ;
order.forEach(current=>
let index = -1;
result.forEach((c,i)=>
if (c.ru==current.ru && c.area==current.area && c.unit==current.unit && c.tot==current.tot && c.equipment==current.equipment && c.parameter==current.parameter)
index = i;

);
// console.log(index);
if(index==-1)
result.push(current);
else
result[index]["tot"] = result[index]["tot"]+current["tot"];

);
console.log(result);





var order = [

"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Speed"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Discharge pressure"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Speed"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"RCU",
"equipment":"37 P 552 B",
"parameter":"Discharge pressure"

];
var result = ;
order.forEach(current=>
let index = -1;
result.forEach((c,i)=>
if (c.ru==current.ru && c.area==current.area && c.unit==current.unit && c.tot==current.tot && c.equipment==current.equipment && c.parameter==current.parameter)
index = i;

);
// console.log(index);
if(index==-1)
result.push(current);
else
result[index]["tot"] = result[index]["tot"]+current["tot"];

);
console.log(result);






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 9 at 4:29









protoproto

94869




94869











  • how to get specific object for example 'ru'?
    – Hamzah Aznageel
    Nov 9 at 4:41










  • Get result just has only "ru"?
    – protoproto
    Nov 9 at 4:44










  • yes, get result only "ru", I think just console.log(result.ru), but the result is undefined
    – Hamzah Aznageel
    Nov 9 at 4:55










  • You can use: console.log(order.map(current=>current["ru"])); --> get only column 'ru'!
    – protoproto
    Nov 9 at 5:03
















  • how to get specific object for example 'ru'?
    – Hamzah Aznageel
    Nov 9 at 4:41










  • Get result just has only "ru"?
    – protoproto
    Nov 9 at 4:44










  • yes, get result only "ru", I think just console.log(result.ru), but the result is undefined
    – Hamzah Aznageel
    Nov 9 at 4:55










  • You can use: console.log(order.map(current=>current["ru"])); --> get only column 'ru'!
    – protoproto
    Nov 9 at 5:03















how to get specific object for example 'ru'?
– Hamzah Aznageel
Nov 9 at 4:41




how to get specific object for example 'ru'?
– Hamzah Aznageel
Nov 9 at 4:41












Get result just has only "ru"?
– protoproto
Nov 9 at 4:44




Get result just has only "ru"?
– protoproto
Nov 9 at 4:44












yes, get result only "ru", I think just console.log(result.ru), but the result is undefined
– Hamzah Aznageel
Nov 9 at 4:55




yes, get result only "ru", I think just console.log(result.ru), but the result is undefined
– Hamzah Aznageel
Nov 9 at 4:55












You can use: console.log(order.map(current=>current["ru"])); --> get only column 'ru'!
– protoproto
Nov 9 at 5:03




You can use: console.log(order.map(current=>current["ru"])); --> get only column 'ru'!
– protoproto
Nov 9 at 5:03












up vote
0
down vote













Maybe you can avoid grouping and directly update the tot property of the merged object in your "index" map/object if you encounter similar elements:



Here is a solution that does that with reduce:






const order = [

"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Speed"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Discharge pressure"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Speed"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"RCU",
"equipment":"37 P 552 B",
"parameter":"Discharge pressure"

]

const merged = Object.values(order.reduce((acc, curr) =>
const key = `$curr.ru#$curr.area#$curr.unit#$curr.equipment#$curr.parameter`;
if (key in acc) acc[key].tot += 1;
else acc[key] = Object.assign(, curr);
return acc;
, ));

console.log(merged);
console.log(merged.map(d => d.ru));








share|improve this answer






















  • how to get all "ru" objects in array?
    – Hamzah Aznageel
    Nov 9 at 5:01










  • @HamzahAznageel it's not that complicated. As @protoproto said, you can use map (see updated answer). merged.map(d => d.ru)
    – slider
    Nov 9 at 5:08










  • well, okay thank you so much
    – Hamzah Aznageel
    Nov 9 at 6:11














up vote
0
down vote













Maybe you can avoid grouping and directly update the tot property of the merged object in your "index" map/object if you encounter similar elements:



Here is a solution that does that with reduce:






const order = [

"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Speed"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Discharge pressure"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Speed"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"RCU",
"equipment":"37 P 552 B",
"parameter":"Discharge pressure"

]

const merged = Object.values(order.reduce((acc, curr) =>
const key = `$curr.ru#$curr.area#$curr.unit#$curr.equipment#$curr.parameter`;
if (key in acc) acc[key].tot += 1;
else acc[key] = Object.assign(, curr);
return acc;
, ));

console.log(merged);
console.log(merged.map(d => d.ru));








share|improve this answer






















  • how to get all "ru" objects in array?
    – Hamzah Aznageel
    Nov 9 at 5:01










  • @HamzahAznageel it's not that complicated. As @protoproto said, you can use map (see updated answer). merged.map(d => d.ru)
    – slider
    Nov 9 at 5:08










  • well, okay thank you so much
    – Hamzah Aznageel
    Nov 9 at 6:11












up vote
0
down vote










up vote
0
down vote









Maybe you can avoid grouping and directly update the tot property of the merged object in your "index" map/object if you encounter similar elements:



Here is a solution that does that with reduce:






const order = [

"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Speed"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Discharge pressure"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Speed"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"RCU",
"equipment":"37 P 552 B",
"parameter":"Discharge pressure"

]

const merged = Object.values(order.reduce((acc, curr) =>
const key = `$curr.ru#$curr.area#$curr.unit#$curr.equipment#$curr.parameter`;
if (key in acc) acc[key].tot += 1;
else acc[key] = Object.assign(, curr);
return acc;
, ));

console.log(merged);
console.log(merged.map(d => d.ru));








share|improve this answer














Maybe you can avoid grouping and directly update the tot property of the merged object in your "index" map/object if you encounter similar elements:



Here is a solution that does that with reduce:






const order = [

"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Speed"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Discharge pressure"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Speed"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"RCU",
"equipment":"37 P 552 B",
"parameter":"Discharge pressure"

]

const merged = Object.values(order.reduce((acc, curr) =>
const key = `$curr.ru#$curr.area#$curr.unit#$curr.equipment#$curr.parameter`;
if (key in acc) acc[key].tot += 1;
else acc[key] = Object.assign(, curr);
return acc;
, ));

console.log(merged);
console.log(merged.map(d => d.ru));








const order = [

"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Speed"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Discharge pressure"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Speed"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"RCU",
"equipment":"37 P 552 B",
"parameter":"Discharge pressure"

]

const merged = Object.values(order.reduce((acc, curr) =>
const key = `$curr.ru#$curr.area#$curr.unit#$curr.equipment#$curr.parameter`;
if (key in acc) acc[key].tot += 1;
else acc[key] = Object.assign(, curr);
return acc;
, ));

console.log(merged);
console.log(merged.map(d => d.ru));





const order = [

"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Speed"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Discharge pressure"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"OFFSITE",
"equipment":"37 P 552 A",
"parameter":"Speed"
,
"tot":1,
"ru":"R401",
"area":"RFCC",
"unit":"RCU",
"equipment":"37 P 552 B",
"parameter":"Discharge pressure"

]

const merged = Object.values(order.reduce((acc, curr) =>
const key = `$curr.ru#$curr.area#$curr.unit#$curr.equipment#$curr.parameter`;
if (key in acc) acc[key].tot += 1;
else acc[key] = Object.assign(, curr);
return acc;
, ));

console.log(merged);
console.log(merged.map(d => d.ru));






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 9 at 5:07

























answered Nov 9 at 4:41









slider

7,6601129




7,6601129











  • how to get all "ru" objects in array?
    – Hamzah Aznageel
    Nov 9 at 5:01










  • @HamzahAznageel it's not that complicated. As @protoproto said, you can use map (see updated answer). merged.map(d => d.ru)
    – slider
    Nov 9 at 5:08










  • well, okay thank you so much
    – Hamzah Aznageel
    Nov 9 at 6:11
















  • how to get all "ru" objects in array?
    – Hamzah Aznageel
    Nov 9 at 5:01










  • @HamzahAznageel it's not that complicated. As @protoproto said, you can use map (see updated answer). merged.map(d => d.ru)
    – slider
    Nov 9 at 5:08










  • well, okay thank you so much
    – Hamzah Aznageel
    Nov 9 at 6:11















how to get all "ru" objects in array?
– Hamzah Aznageel
Nov 9 at 5:01




how to get all "ru" objects in array?
– Hamzah Aznageel
Nov 9 at 5:01












@HamzahAznageel it's not that complicated. As @protoproto said, you can use map (see updated answer). merged.map(d => d.ru)
– slider
Nov 9 at 5:08




@HamzahAznageel it's not that complicated. As @protoproto said, you can use map (see updated answer). merged.map(d => d.ru)
– slider
Nov 9 at 5:08












well, okay thank you so much
– Hamzah Aznageel
Nov 9 at 6:11




well, okay thank you so much
– Hamzah Aznageel
Nov 9 at 6:11

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53219628%2fgrouping-elements-in-array-by-multiple-properties-underscore%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)