Grouping elements in array by multiple properties underscore
up vote
0
down vote
favorite
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
add a comment |
up vote
0
down vote
favorite
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
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
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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
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
javascript arrays json underscore.js
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
add a comment |
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
add a comment |
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);
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
add a comment |
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));
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 usemap
(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
add a comment |
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);
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
add a comment |
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);
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
add a comment |
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);
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);
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
add a comment |
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
add a comment |
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));
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 usemap
(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
add a comment |
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));
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 usemap
(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
add a comment |
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));
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));
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 usemap
(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
add a comment |
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 usemap
(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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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