Reduce Array based on properties of 2 keys
I have an array like this.
var data = [{
name: 'Apple',
category: 'Fruit',
value: 12
}, {
name: 'Apple',
category: 'Fruit',
value: 20
}, {
name: 'Orange',
category: 'Fruit',
value: 65
}, {
name: 'Orange',
category: 'Fruit',
value: 40
}]
I need to get an array like this:
var newData = [{
name: 'Apple',
category: 'Fruit',
value: 20
}, {
name: 'Orange',
category: 'Fruit',
value: 65
}]
That is get the highest value for each 'name' key in an array.
This is what I have tried:
var data = [{
name: 'Apple',
category: 'Fruit',
value: 12
}, {
name: 'Apple',
category: 'Fruit',
value: 20
}, {
name: 'Orange',
category: 'Fruit',
value: 65
}, {
name: 'Orange',
category: 'Fruit',
value: 40
}]
console.log(data.reduce(function(prev, current) {
return (prev.name !== current.name && prev.value > current.value) ? prev : current
}))
Please advice.
javascript arrays object
add a comment |
I have an array like this.
var data = [{
name: 'Apple',
category: 'Fruit',
value: 12
}, {
name: 'Apple',
category: 'Fruit',
value: 20
}, {
name: 'Orange',
category: 'Fruit',
value: 65
}, {
name: 'Orange',
category: 'Fruit',
value: 40
}]
I need to get an array like this:
var newData = [{
name: 'Apple',
category: 'Fruit',
value: 20
}, {
name: 'Orange',
category: 'Fruit',
value: 65
}]
That is get the highest value for each 'name' key in an array.
This is what I have tried:
var data = [{
name: 'Apple',
category: 'Fruit',
value: 12
}, {
name: 'Apple',
category: 'Fruit',
value: 20
}, {
name: 'Orange',
category: 'Fruit',
value: 65
}, {
name: 'Orange',
category: 'Fruit',
value: 40
}]
console.log(data.reduce(function(prev, current) {
return (prev.name !== current.name && prev.value > current.value) ? prev : current
}))
Please advice.
javascript arrays object
add a comment |
I have an array like this.
var data = [{
name: 'Apple',
category: 'Fruit',
value: 12
}, {
name: 'Apple',
category: 'Fruit',
value: 20
}, {
name: 'Orange',
category: 'Fruit',
value: 65
}, {
name: 'Orange',
category: 'Fruit',
value: 40
}]
I need to get an array like this:
var newData = [{
name: 'Apple',
category: 'Fruit',
value: 20
}, {
name: 'Orange',
category: 'Fruit',
value: 65
}]
That is get the highest value for each 'name' key in an array.
This is what I have tried:
var data = [{
name: 'Apple',
category: 'Fruit',
value: 12
}, {
name: 'Apple',
category: 'Fruit',
value: 20
}, {
name: 'Orange',
category: 'Fruit',
value: 65
}, {
name: 'Orange',
category: 'Fruit',
value: 40
}]
console.log(data.reduce(function(prev, current) {
return (prev.name !== current.name && prev.value > current.value) ? prev : current
}))
Please advice.
javascript arrays object
I have an array like this.
var data = [{
name: 'Apple',
category: 'Fruit',
value: 12
}, {
name: 'Apple',
category: 'Fruit',
value: 20
}, {
name: 'Orange',
category: 'Fruit',
value: 65
}, {
name: 'Orange',
category: 'Fruit',
value: 40
}]
I need to get an array like this:
var newData = [{
name: 'Apple',
category: 'Fruit',
value: 20
}, {
name: 'Orange',
category: 'Fruit',
value: 65
}]
That is get the highest value for each 'name' key in an array.
This is what I have tried:
var data = [{
name: 'Apple',
category: 'Fruit',
value: 12
}, {
name: 'Apple',
category: 'Fruit',
value: 20
}, {
name: 'Orange',
category: 'Fruit',
value: 65
}, {
name: 'Orange',
category: 'Fruit',
value: 40
}]
console.log(data.reduce(function(prev, current) {
return (prev.name !== current.name && prev.value > current.value) ? prev : current
}))
Please advice.
var data = [{
name: 'Apple',
category: 'Fruit',
value: 12
}, {
name: 'Apple',
category: 'Fruit',
value: 20
}, {
name: 'Orange',
category: 'Fruit',
value: 65
}, {
name: 'Orange',
category: 'Fruit',
value: 40
}]
console.log(data.reduce(function(prev, current) {
return (prev.name !== current.name && prev.value > current.value) ? prev : current
}))
var data = [{
name: 'Apple',
category: 'Fruit',
value: 12
}, {
name: 'Apple',
category: 'Fruit',
value: 20
}, {
name: 'Orange',
category: 'Fruit',
value: 65
}, {
name: 'Orange',
category: 'Fruit',
value: 40
}]
console.log(data.reduce(function(prev, current) {
return (prev.name !== current.name && prev.value > current.value) ? prev : current
}))
javascript arrays object
javascript arrays object
asked Nov 14 '18 at 3:41
a2441918a2441918
635213
635213
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
You need to keep track of the current highest value
items for every name
, so you could try reducing into an object, whose keys are the name
s, and values are the currently winning value
s for that name. Then, get the resulting object's values:
var data = [{
name: 'Apple',
category: 'Fruit',
value: 12
}, {
name: 'Apple',
category: 'Fruit',
value: 20
}, {
name: 'Orange',
category: 'Fruit',
value: 65
}, {
name: 'Orange',
category: 'Fruit',
value: 40
}];
console.log(Object.values(
data.reduce((a, item) => {
const { name, value } = item;
if (!a[name] || a[name].value < value) a[name] = item;
return a;
}, {})
));
(Your prev.name !== current.name ...
method would only work if you're only trying to get one item out of it in the end - because you need more, you need an object that can contain multiple items.)
add a comment |
Use reduce and check if the accumulator array has the same fruit using findIndex
. If it is -1 , then in accumulator array push the value else update the count in that index
var data = [{
name: 'Apple',
category: 'Fruit',
value: 12
}, {
name: 'Apple',
category: 'Fruit',
value: 20
}, {
name: 'Orange',
category: 'Fruit',
value: 65
}, {
name: 'Orange',
category: 'Fruit',
value: 40
}]
let result = data.reduce(function(acc, curr) {
let findIfExist = acc.findIndex(elem => {
return elem.name === curr.name;
})
if (findIfExist === -1) {
acc.push(curr)
} else {
if (acc[findIfExist].value < curr.value) {
acc[findIfExist].value = curr.value
}
}
return acc;
}, )
console.log(result)
add a comment |
Use forEach
instead of reduce
var data = [{
name: 'Apple',
category: 'Fruit',
value: 12
}, {
name: 'Apple',
category: 'Fruit',
value: 20
}, {
name: 'Orange',
category: 'Fruit',
value: 65
}, {
name: 'Orange',
category: 'Fruit',
value: 40
}]
let result = {};
data.forEach(function(current) {
if (result[current.name] &&
result[current.name].value < current.value &&
result[current.name].category === current.category) {
result[current.name].value = current.value;
} else if (!result[current.name]) {
result[current.name] = current;
}
});
console.log(Object.values(result));
is there any reason to use forEach instead of reduce ?
– brk
Nov 14 '18 at 3:54
@brk No specific reason.. CertainPerformance answer looks great as well..
– Ramesh
Nov 14 '18 at 4:22
add a comment |
var data = [{
name: 'Apple',
category: 'Fruit',
value: 12
}, {
name: 'Apple',
category: 'Fruit',
value: 20
}, {
name: 'Orange',
category: 'Fruit',
value: 65
}, {
name: 'Orange',
category: 'Fruit',
value: 40
}]
let result=data.reduce(function(temp, current) {
//check if temp has object
var found = temp.some((el) => {
return el.name === current.name
});
//if not found, add the object
if (!found) { temp.push(current) }
//if found and value is greater, replace the max value
else{
temp.find((element) => {
if(element.name === current.name && element.value<current.value)
element.value=current.value
})
}
return temp
},)
console.log(result);
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
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%2f53292876%2freduce-array-based-on-properties-of-2-keys%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
You need to keep track of the current highest value
items for every name
, so you could try reducing into an object, whose keys are the name
s, and values are the currently winning value
s for that name. Then, get the resulting object's values:
var data = [{
name: 'Apple',
category: 'Fruit',
value: 12
}, {
name: 'Apple',
category: 'Fruit',
value: 20
}, {
name: 'Orange',
category: 'Fruit',
value: 65
}, {
name: 'Orange',
category: 'Fruit',
value: 40
}];
console.log(Object.values(
data.reduce((a, item) => {
const { name, value } = item;
if (!a[name] || a[name].value < value) a[name] = item;
return a;
}, {})
));
(Your prev.name !== current.name ...
method would only work if you're only trying to get one item out of it in the end - because you need more, you need an object that can contain multiple items.)
add a comment |
You need to keep track of the current highest value
items for every name
, so you could try reducing into an object, whose keys are the name
s, and values are the currently winning value
s for that name. Then, get the resulting object's values:
var data = [{
name: 'Apple',
category: 'Fruit',
value: 12
}, {
name: 'Apple',
category: 'Fruit',
value: 20
}, {
name: 'Orange',
category: 'Fruit',
value: 65
}, {
name: 'Orange',
category: 'Fruit',
value: 40
}];
console.log(Object.values(
data.reduce((a, item) => {
const { name, value } = item;
if (!a[name] || a[name].value < value) a[name] = item;
return a;
}, {})
));
(Your prev.name !== current.name ...
method would only work if you're only trying to get one item out of it in the end - because you need more, you need an object that can contain multiple items.)
add a comment |
You need to keep track of the current highest value
items for every name
, so you could try reducing into an object, whose keys are the name
s, and values are the currently winning value
s for that name. Then, get the resulting object's values:
var data = [{
name: 'Apple',
category: 'Fruit',
value: 12
}, {
name: 'Apple',
category: 'Fruit',
value: 20
}, {
name: 'Orange',
category: 'Fruit',
value: 65
}, {
name: 'Orange',
category: 'Fruit',
value: 40
}];
console.log(Object.values(
data.reduce((a, item) => {
const { name, value } = item;
if (!a[name] || a[name].value < value) a[name] = item;
return a;
}, {})
));
(Your prev.name !== current.name ...
method would only work if you're only trying to get one item out of it in the end - because you need more, you need an object that can contain multiple items.)
You need to keep track of the current highest value
items for every name
, so you could try reducing into an object, whose keys are the name
s, and values are the currently winning value
s for that name. Then, get the resulting object's values:
var data = [{
name: 'Apple',
category: 'Fruit',
value: 12
}, {
name: 'Apple',
category: 'Fruit',
value: 20
}, {
name: 'Orange',
category: 'Fruit',
value: 65
}, {
name: 'Orange',
category: 'Fruit',
value: 40
}];
console.log(Object.values(
data.reduce((a, item) => {
const { name, value } = item;
if (!a[name] || a[name].value < value) a[name] = item;
return a;
}, {})
));
(Your prev.name !== current.name ...
method would only work if you're only trying to get one item out of it in the end - because you need more, you need an object that can contain multiple items.)
var data = [{
name: 'Apple',
category: 'Fruit',
value: 12
}, {
name: 'Apple',
category: 'Fruit',
value: 20
}, {
name: 'Orange',
category: 'Fruit',
value: 65
}, {
name: 'Orange',
category: 'Fruit',
value: 40
}];
console.log(Object.values(
data.reduce((a, item) => {
const { name, value } = item;
if (!a[name] || a[name].value < value) a[name] = item;
return a;
}, {})
));
var data = [{
name: 'Apple',
category: 'Fruit',
value: 12
}, {
name: 'Apple',
category: 'Fruit',
value: 20
}, {
name: 'Orange',
category: 'Fruit',
value: 65
}, {
name: 'Orange',
category: 'Fruit',
value: 40
}];
console.log(Object.values(
data.reduce((a, item) => {
const { name, value } = item;
if (!a[name] || a[name].value < value) a[name] = item;
return a;
}, {})
));
edited Nov 14 '18 at 3:45
answered Nov 14 '18 at 3:44
CertainPerformanceCertainPerformance
84.9k154169
84.9k154169
add a comment |
add a comment |
Use reduce and check if the accumulator array has the same fruit using findIndex
. If it is -1 , then in accumulator array push the value else update the count in that index
var data = [{
name: 'Apple',
category: 'Fruit',
value: 12
}, {
name: 'Apple',
category: 'Fruit',
value: 20
}, {
name: 'Orange',
category: 'Fruit',
value: 65
}, {
name: 'Orange',
category: 'Fruit',
value: 40
}]
let result = data.reduce(function(acc, curr) {
let findIfExist = acc.findIndex(elem => {
return elem.name === curr.name;
})
if (findIfExist === -1) {
acc.push(curr)
} else {
if (acc[findIfExist].value < curr.value) {
acc[findIfExist].value = curr.value
}
}
return acc;
}, )
console.log(result)
add a comment |
Use reduce and check if the accumulator array has the same fruit using findIndex
. If it is -1 , then in accumulator array push the value else update the count in that index
var data = [{
name: 'Apple',
category: 'Fruit',
value: 12
}, {
name: 'Apple',
category: 'Fruit',
value: 20
}, {
name: 'Orange',
category: 'Fruit',
value: 65
}, {
name: 'Orange',
category: 'Fruit',
value: 40
}]
let result = data.reduce(function(acc, curr) {
let findIfExist = acc.findIndex(elem => {
return elem.name === curr.name;
})
if (findIfExist === -1) {
acc.push(curr)
} else {
if (acc[findIfExist].value < curr.value) {
acc[findIfExist].value = curr.value
}
}
return acc;
}, )
console.log(result)
add a comment |
Use reduce and check if the accumulator array has the same fruit using findIndex
. If it is -1 , then in accumulator array push the value else update the count in that index
var data = [{
name: 'Apple',
category: 'Fruit',
value: 12
}, {
name: 'Apple',
category: 'Fruit',
value: 20
}, {
name: 'Orange',
category: 'Fruit',
value: 65
}, {
name: 'Orange',
category: 'Fruit',
value: 40
}]
let result = data.reduce(function(acc, curr) {
let findIfExist = acc.findIndex(elem => {
return elem.name === curr.name;
})
if (findIfExist === -1) {
acc.push(curr)
} else {
if (acc[findIfExist].value < curr.value) {
acc[findIfExist].value = curr.value
}
}
return acc;
}, )
console.log(result)
Use reduce and check if the accumulator array has the same fruit using findIndex
. If it is -1 , then in accumulator array push the value else update the count in that index
var data = [{
name: 'Apple',
category: 'Fruit',
value: 12
}, {
name: 'Apple',
category: 'Fruit',
value: 20
}, {
name: 'Orange',
category: 'Fruit',
value: 65
}, {
name: 'Orange',
category: 'Fruit',
value: 40
}]
let result = data.reduce(function(acc, curr) {
let findIfExist = acc.findIndex(elem => {
return elem.name === curr.name;
})
if (findIfExist === -1) {
acc.push(curr)
} else {
if (acc[findIfExist].value < curr.value) {
acc[findIfExist].value = curr.value
}
}
return acc;
}, )
console.log(result)
var data = [{
name: 'Apple',
category: 'Fruit',
value: 12
}, {
name: 'Apple',
category: 'Fruit',
value: 20
}, {
name: 'Orange',
category: 'Fruit',
value: 65
}, {
name: 'Orange',
category: 'Fruit',
value: 40
}]
let result = data.reduce(function(acc, curr) {
let findIfExist = acc.findIndex(elem => {
return elem.name === curr.name;
})
if (findIfExist === -1) {
acc.push(curr)
} else {
if (acc[findIfExist].value < curr.value) {
acc[findIfExist].value = curr.value
}
}
return acc;
}, )
console.log(result)
var data = [{
name: 'Apple',
category: 'Fruit',
value: 12
}, {
name: 'Apple',
category: 'Fruit',
value: 20
}, {
name: 'Orange',
category: 'Fruit',
value: 65
}, {
name: 'Orange',
category: 'Fruit',
value: 40
}]
let result = data.reduce(function(acc, curr) {
let findIfExist = acc.findIndex(elem => {
return elem.name === curr.name;
})
if (findIfExist === -1) {
acc.push(curr)
} else {
if (acc[findIfExist].value < curr.value) {
acc[findIfExist].value = curr.value
}
}
return acc;
}, )
console.log(result)
answered Nov 14 '18 at 3:47
brkbrk
26.5k32040
26.5k32040
add a comment |
add a comment |
Use forEach
instead of reduce
var data = [{
name: 'Apple',
category: 'Fruit',
value: 12
}, {
name: 'Apple',
category: 'Fruit',
value: 20
}, {
name: 'Orange',
category: 'Fruit',
value: 65
}, {
name: 'Orange',
category: 'Fruit',
value: 40
}]
let result = {};
data.forEach(function(current) {
if (result[current.name] &&
result[current.name].value < current.value &&
result[current.name].category === current.category) {
result[current.name].value = current.value;
} else if (!result[current.name]) {
result[current.name] = current;
}
});
console.log(Object.values(result));
is there any reason to use forEach instead of reduce ?
– brk
Nov 14 '18 at 3:54
@brk No specific reason.. CertainPerformance answer looks great as well..
– Ramesh
Nov 14 '18 at 4:22
add a comment |
Use forEach
instead of reduce
var data = [{
name: 'Apple',
category: 'Fruit',
value: 12
}, {
name: 'Apple',
category: 'Fruit',
value: 20
}, {
name: 'Orange',
category: 'Fruit',
value: 65
}, {
name: 'Orange',
category: 'Fruit',
value: 40
}]
let result = {};
data.forEach(function(current) {
if (result[current.name] &&
result[current.name].value < current.value &&
result[current.name].category === current.category) {
result[current.name].value = current.value;
} else if (!result[current.name]) {
result[current.name] = current;
}
});
console.log(Object.values(result));
is there any reason to use forEach instead of reduce ?
– brk
Nov 14 '18 at 3:54
@brk No specific reason.. CertainPerformance answer looks great as well..
– Ramesh
Nov 14 '18 at 4:22
add a comment |
Use forEach
instead of reduce
var data = [{
name: 'Apple',
category: 'Fruit',
value: 12
}, {
name: 'Apple',
category: 'Fruit',
value: 20
}, {
name: 'Orange',
category: 'Fruit',
value: 65
}, {
name: 'Orange',
category: 'Fruit',
value: 40
}]
let result = {};
data.forEach(function(current) {
if (result[current.name] &&
result[current.name].value < current.value &&
result[current.name].category === current.category) {
result[current.name].value = current.value;
} else if (!result[current.name]) {
result[current.name] = current;
}
});
console.log(Object.values(result));
Use forEach
instead of reduce
var data = [{
name: 'Apple',
category: 'Fruit',
value: 12
}, {
name: 'Apple',
category: 'Fruit',
value: 20
}, {
name: 'Orange',
category: 'Fruit',
value: 65
}, {
name: 'Orange',
category: 'Fruit',
value: 40
}]
let result = {};
data.forEach(function(current) {
if (result[current.name] &&
result[current.name].value < current.value &&
result[current.name].category === current.category) {
result[current.name].value = current.value;
} else if (!result[current.name]) {
result[current.name] = current;
}
});
console.log(Object.values(result));
var data = [{
name: 'Apple',
category: 'Fruit',
value: 12
}, {
name: 'Apple',
category: 'Fruit',
value: 20
}, {
name: 'Orange',
category: 'Fruit',
value: 65
}, {
name: 'Orange',
category: 'Fruit',
value: 40
}]
let result = {};
data.forEach(function(current) {
if (result[current.name] &&
result[current.name].value < current.value &&
result[current.name].category === current.category) {
result[current.name].value = current.value;
} else if (!result[current.name]) {
result[current.name] = current;
}
});
console.log(Object.values(result));
var data = [{
name: 'Apple',
category: 'Fruit',
value: 12
}, {
name: 'Apple',
category: 'Fruit',
value: 20
}, {
name: 'Orange',
category: 'Fruit',
value: 65
}, {
name: 'Orange',
category: 'Fruit',
value: 40
}]
let result = {};
data.forEach(function(current) {
if (result[current.name] &&
result[current.name].value < current.value &&
result[current.name].category === current.category) {
result[current.name].value = current.value;
} else if (!result[current.name]) {
result[current.name] = current;
}
});
console.log(Object.values(result));
edited Nov 14 '18 at 4:20
answered Nov 14 '18 at 3:51
RameshRamesh
9,94623777
9,94623777
is there any reason to use forEach instead of reduce ?
– brk
Nov 14 '18 at 3:54
@brk No specific reason.. CertainPerformance answer looks great as well..
– Ramesh
Nov 14 '18 at 4:22
add a comment |
is there any reason to use forEach instead of reduce ?
– brk
Nov 14 '18 at 3:54
@brk No specific reason.. CertainPerformance answer looks great as well..
– Ramesh
Nov 14 '18 at 4:22
is there any reason to use forEach instead of reduce ?
– brk
Nov 14 '18 at 3:54
is there any reason to use forEach instead of reduce ?
– brk
Nov 14 '18 at 3:54
@brk No specific reason.. CertainPerformance answer looks great as well..
– Ramesh
Nov 14 '18 at 4:22
@brk No specific reason.. CertainPerformance answer looks great as well..
– Ramesh
Nov 14 '18 at 4:22
add a comment |
var data = [{
name: 'Apple',
category: 'Fruit',
value: 12
}, {
name: 'Apple',
category: 'Fruit',
value: 20
}, {
name: 'Orange',
category: 'Fruit',
value: 65
}, {
name: 'Orange',
category: 'Fruit',
value: 40
}]
let result=data.reduce(function(temp, current) {
//check if temp has object
var found = temp.some((el) => {
return el.name === current.name
});
//if not found, add the object
if (!found) { temp.push(current) }
//if found and value is greater, replace the max value
else{
temp.find((element) => {
if(element.name === current.name && element.value<current.value)
element.value=current.value
})
}
return temp
},)
console.log(result);
add a comment |
var data = [{
name: 'Apple',
category: 'Fruit',
value: 12
}, {
name: 'Apple',
category: 'Fruit',
value: 20
}, {
name: 'Orange',
category: 'Fruit',
value: 65
}, {
name: 'Orange',
category: 'Fruit',
value: 40
}]
let result=data.reduce(function(temp, current) {
//check if temp has object
var found = temp.some((el) => {
return el.name === current.name
});
//if not found, add the object
if (!found) { temp.push(current) }
//if found and value is greater, replace the max value
else{
temp.find((element) => {
if(element.name === current.name && element.value<current.value)
element.value=current.value
})
}
return temp
},)
console.log(result);
add a comment |
var data = [{
name: 'Apple',
category: 'Fruit',
value: 12
}, {
name: 'Apple',
category: 'Fruit',
value: 20
}, {
name: 'Orange',
category: 'Fruit',
value: 65
}, {
name: 'Orange',
category: 'Fruit',
value: 40
}]
let result=data.reduce(function(temp, current) {
//check if temp has object
var found = temp.some((el) => {
return el.name === current.name
});
//if not found, add the object
if (!found) { temp.push(current) }
//if found and value is greater, replace the max value
else{
temp.find((element) => {
if(element.name === current.name && element.value<current.value)
element.value=current.value
})
}
return temp
},)
console.log(result);
var data = [{
name: 'Apple',
category: 'Fruit',
value: 12
}, {
name: 'Apple',
category: 'Fruit',
value: 20
}, {
name: 'Orange',
category: 'Fruit',
value: 65
}, {
name: 'Orange',
category: 'Fruit',
value: 40
}]
let result=data.reduce(function(temp, current) {
//check if temp has object
var found = temp.some((el) => {
return el.name === current.name
});
//if not found, add the object
if (!found) { temp.push(current) }
//if found and value is greater, replace the max value
else{
temp.find((element) => {
if(element.name === current.name && element.value<current.value)
element.value=current.value
})
}
return temp
},)
console.log(result);
var data = [{
name: 'Apple',
category: 'Fruit',
value: 12
}, {
name: 'Apple',
category: 'Fruit',
value: 20
}, {
name: 'Orange',
category: 'Fruit',
value: 65
}, {
name: 'Orange',
category: 'Fruit',
value: 40
}]
let result=data.reduce(function(temp, current) {
//check if temp has object
var found = temp.some((el) => {
return el.name === current.name
});
//if not found, add the object
if (!found) { temp.push(current) }
//if found and value is greater, replace the max value
else{
temp.find((element) => {
if(element.name === current.name && element.value<current.value)
element.value=current.value
})
}
return temp
},)
console.log(result);
var data = [{
name: 'Apple',
category: 'Fruit',
value: 12
}, {
name: 'Apple',
category: 'Fruit',
value: 20
}, {
name: 'Orange',
category: 'Fruit',
value: 65
}, {
name: 'Orange',
category: 'Fruit',
value: 40
}]
let result=data.reduce(function(temp, current) {
//check if temp has object
var found = temp.some((el) => {
return el.name === current.name
});
//if not found, add the object
if (!found) { temp.push(current) }
//if found and value is greater, replace the max value
else{
temp.find((element) => {
if(element.name === current.name && element.value<current.value)
element.value=current.value
})
}
return temp
},)
console.log(result);
edited Nov 14 '18 at 12:16
answered Nov 14 '18 at 11:45
Monica AchaMonica Acha
33219
33219
add a comment |
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.
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%2f53292876%2freduce-array-based-on-properties-of-2-keys%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