Reduce Array based on properties of 2 keys












0















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.










share|improve this question



























    0















    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.










    share|improve this question

























      0












      0








      0








      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.










      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 3:41









      a2441918a2441918

      635213




      635213
























          4 Answers
          4






          active

          oldest

          votes


















          4














          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 names, and values are the currently winning values 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.)






          share|improve this answer

































            0














            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)








            share|improve this answer































              0














              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));








              share|improve this answer


























              • 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



















              0

















              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);








              share|improve this answer

























                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
                });


                }
                });














                draft saved

                draft discarded


















                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









                4














                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 names, and values are the currently winning values 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.)






                share|improve this answer






























                  4














                  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 names, and values are the currently winning values 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.)






                  share|improve this answer




























                    4












                    4








                    4







                    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 names, and values are the currently winning values 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.)






                    share|improve this answer















                    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 names, and values are the currently winning values 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;
                    }, {})
                    ));






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 14 '18 at 3:45

























                    answered Nov 14 '18 at 3:44









                    CertainPerformanceCertainPerformance

                    84.9k154169




                    84.9k154169

























                        0














                        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)








                        share|improve this answer




























                          0














                          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)








                          share|improve this answer


























                            0












                            0








                            0







                            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)








                            share|improve this answer













                            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)






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 14 '18 at 3:47









                            brkbrk

                            26.5k32040




                            26.5k32040























                                0














                                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));








                                share|improve this answer


























                                • 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
















                                0














                                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));








                                share|improve this answer


























                                • 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














                                0












                                0








                                0







                                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));








                                share|improve this answer















                                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));






                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                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



















                                • 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











                                0

















                                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);








                                share|improve this answer






























                                  0

















                                  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);








                                  share|improve this answer




























                                    0












                                    0








                                    0










                                    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);








                                    share|improve this answer


















                                    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);






                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Nov 14 '18 at 12:16

























                                    answered Nov 14 '18 at 11:45









                                    Monica AchaMonica Acha

                                    33219




                                    33219






























                                        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.




                                        draft saved


                                        draft discarded














                                        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





















































                                        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

                                        Florida Star v. B. J. F.

                                        Danny Elfman

                                        Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues