Get closest greater value in multiple attributes of Array of Objects











up vote
3
down vote

favorite












I have this array of objects.



var priceArray = [ 
{ "foo": 1, "bar": 2, "price": 3 },
{ "foo": 2, "bar": 123, "price": 124 },
{ "foo": 2, "bar": 2, "price": 5 },
{ "foo": 112, "bar": 2, "price": 75 },
{ "foo": 2, "bar": 3, "price": 5 },
{ "foo": 3, "bar": 2, "price": 3 },
{ "foo": 3, "bar": 4, "price": 7 },
{ "foo": 4, "bar": 3, "price": 4 },
{ "foo": 4, "bar": 4, "price": 6 }
];


I have another array of objects.



var dataArray = [ 
{ foo: 0.25, bar: 1 },
{ foo: 0.5, bar: 1 },
{ foo: 1, bar: 1 },
{ foo: 1, bar: 2 },
{ foo: 2, bar: 1 },
{ foo: 2, bar: 2 },
{ foo: 2, bar: 4 },
{ foo: 3, bar: 2 },
{ foo: 4, bar: 1 },
{ foo: 4, bar: 2 },
{ foo: 4, bar: 4 },
];


I want to add price in dataArray which price row foo and bar values are closest greater in priceArray.



for example - filtered data should like this



var filterDataArray = [ 
{ foo: 0.25, bar: 1, price: 3 },
{ foo: 0.5, bar: 1, price: 3 },
{ foo: 1, bar: 1, price: 3 },
{ foo: 1, bar: 2, price: 3 },
{ foo: 2, bar: 1, price: 5 },
{ foo: 2, bar: 2, price: 5 },
{ foo: 2, bar: 4, price: 7 },
{ foo: 3, bar: 2, price: 3 },
{ foo: 4, bar: 1, price: 4 },
{ foo: 4, bar: 2, price: 4 },
{ foo: 4, bar: 4, price: 6 },
];


If I sort priceArray with foo and bar value with help of these codes and then filter this array with dataArray. So, I didn't get my solution. like -



priceArray = priceArray.sort(function(a, b) {
return a.foo - b.foo || a.bar - b.bar;
});
or
priceArray = _.sortBy(( _.sortBy(priceArray, 'bar')), 'foo');
or
priceArray = _.orderBy(priceArray, ['foo', 'bar'], ['asc', 'asc']);

// create new data array with price
const dataNewArray = ;
dataArray.map(dataObj => {
const dataNewObj = {};
for(const priceObject of priceArray) {
if (dataObj.foo <= priceObject.foo && dataObj.bar <= priceObject.bar) {
dataNewObj.foo = dataObj.foo;
dataNewObj.bar = dataObj.bar;
dataNewObj.price = priceObject.price;
break;
}
}
dataNewArray.push(dataNewObj);
});


but it's solution is



dataNewArray = [ 
{ foo: 0.25, bar: 1, price: 3 },
{ foo: 0.5, bar: 1, price: 3 },
{ foo: 1, bar: 1, price: 3 },
{ foo: 1, bar: 2, price: 3 },
{ foo: 2, bar: 1, price: 5 },
{ foo: 2, bar: 2, price: 5 },
{ foo: 2, bar: 4, price: 124 },
{ foo: 3, bar: 2, price: 3 },
{ foo: 4, bar: 1, price: 4 },
{ foo: 4, bar: 2, price: 4 },
{ foo: 4, bar: 4, price: 6 }
];


So, you can see in this object { foo: 2, bar: 4, price: 124 }, price should be 7, not 124.



How to solve my problem with help of anything js features like Lodash, or any algorithm. I tried many solutions at StackOverflow but maximum solutions for single attribute in object.










share|improve this question


























    up vote
    3
    down vote

    favorite












    I have this array of objects.



    var priceArray = [ 
    { "foo": 1, "bar": 2, "price": 3 },
    { "foo": 2, "bar": 123, "price": 124 },
    { "foo": 2, "bar": 2, "price": 5 },
    { "foo": 112, "bar": 2, "price": 75 },
    { "foo": 2, "bar": 3, "price": 5 },
    { "foo": 3, "bar": 2, "price": 3 },
    { "foo": 3, "bar": 4, "price": 7 },
    { "foo": 4, "bar": 3, "price": 4 },
    { "foo": 4, "bar": 4, "price": 6 }
    ];


    I have another array of objects.



    var dataArray = [ 
    { foo: 0.25, bar: 1 },
    { foo: 0.5, bar: 1 },
    { foo: 1, bar: 1 },
    { foo: 1, bar: 2 },
    { foo: 2, bar: 1 },
    { foo: 2, bar: 2 },
    { foo: 2, bar: 4 },
    { foo: 3, bar: 2 },
    { foo: 4, bar: 1 },
    { foo: 4, bar: 2 },
    { foo: 4, bar: 4 },
    ];


    I want to add price in dataArray which price row foo and bar values are closest greater in priceArray.



    for example - filtered data should like this



    var filterDataArray = [ 
    { foo: 0.25, bar: 1, price: 3 },
    { foo: 0.5, bar: 1, price: 3 },
    { foo: 1, bar: 1, price: 3 },
    { foo: 1, bar: 2, price: 3 },
    { foo: 2, bar: 1, price: 5 },
    { foo: 2, bar: 2, price: 5 },
    { foo: 2, bar: 4, price: 7 },
    { foo: 3, bar: 2, price: 3 },
    { foo: 4, bar: 1, price: 4 },
    { foo: 4, bar: 2, price: 4 },
    { foo: 4, bar: 4, price: 6 },
    ];


    If I sort priceArray with foo and bar value with help of these codes and then filter this array with dataArray. So, I didn't get my solution. like -



    priceArray = priceArray.sort(function(a, b) {
    return a.foo - b.foo || a.bar - b.bar;
    });
    or
    priceArray = _.sortBy(( _.sortBy(priceArray, 'bar')), 'foo');
    or
    priceArray = _.orderBy(priceArray, ['foo', 'bar'], ['asc', 'asc']);

    // create new data array with price
    const dataNewArray = ;
    dataArray.map(dataObj => {
    const dataNewObj = {};
    for(const priceObject of priceArray) {
    if (dataObj.foo <= priceObject.foo && dataObj.bar <= priceObject.bar) {
    dataNewObj.foo = dataObj.foo;
    dataNewObj.bar = dataObj.bar;
    dataNewObj.price = priceObject.price;
    break;
    }
    }
    dataNewArray.push(dataNewObj);
    });


    but it's solution is



    dataNewArray = [ 
    { foo: 0.25, bar: 1, price: 3 },
    { foo: 0.5, bar: 1, price: 3 },
    { foo: 1, bar: 1, price: 3 },
    { foo: 1, bar: 2, price: 3 },
    { foo: 2, bar: 1, price: 5 },
    { foo: 2, bar: 2, price: 5 },
    { foo: 2, bar: 4, price: 124 },
    { foo: 3, bar: 2, price: 3 },
    { foo: 4, bar: 1, price: 4 },
    { foo: 4, bar: 2, price: 4 },
    { foo: 4, bar: 4, price: 6 }
    ];


    So, you can see in this object { foo: 2, bar: 4, price: 124 }, price should be 7, not 124.



    How to solve my problem with help of anything js features like Lodash, or any algorithm. I tried many solutions at StackOverflow but maximum solutions for single attribute in object.










    share|improve this question
























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      I have this array of objects.



      var priceArray = [ 
      { "foo": 1, "bar": 2, "price": 3 },
      { "foo": 2, "bar": 123, "price": 124 },
      { "foo": 2, "bar": 2, "price": 5 },
      { "foo": 112, "bar": 2, "price": 75 },
      { "foo": 2, "bar": 3, "price": 5 },
      { "foo": 3, "bar": 2, "price": 3 },
      { "foo": 3, "bar": 4, "price": 7 },
      { "foo": 4, "bar": 3, "price": 4 },
      { "foo": 4, "bar": 4, "price": 6 }
      ];


      I have another array of objects.



      var dataArray = [ 
      { foo: 0.25, bar: 1 },
      { foo: 0.5, bar: 1 },
      { foo: 1, bar: 1 },
      { foo: 1, bar: 2 },
      { foo: 2, bar: 1 },
      { foo: 2, bar: 2 },
      { foo: 2, bar: 4 },
      { foo: 3, bar: 2 },
      { foo: 4, bar: 1 },
      { foo: 4, bar: 2 },
      { foo: 4, bar: 4 },
      ];


      I want to add price in dataArray which price row foo and bar values are closest greater in priceArray.



      for example - filtered data should like this



      var filterDataArray = [ 
      { foo: 0.25, bar: 1, price: 3 },
      { foo: 0.5, bar: 1, price: 3 },
      { foo: 1, bar: 1, price: 3 },
      { foo: 1, bar: 2, price: 3 },
      { foo: 2, bar: 1, price: 5 },
      { foo: 2, bar: 2, price: 5 },
      { foo: 2, bar: 4, price: 7 },
      { foo: 3, bar: 2, price: 3 },
      { foo: 4, bar: 1, price: 4 },
      { foo: 4, bar: 2, price: 4 },
      { foo: 4, bar: 4, price: 6 },
      ];


      If I sort priceArray with foo and bar value with help of these codes and then filter this array with dataArray. So, I didn't get my solution. like -



      priceArray = priceArray.sort(function(a, b) {
      return a.foo - b.foo || a.bar - b.bar;
      });
      or
      priceArray = _.sortBy(( _.sortBy(priceArray, 'bar')), 'foo');
      or
      priceArray = _.orderBy(priceArray, ['foo', 'bar'], ['asc', 'asc']);

      // create new data array with price
      const dataNewArray = ;
      dataArray.map(dataObj => {
      const dataNewObj = {};
      for(const priceObject of priceArray) {
      if (dataObj.foo <= priceObject.foo && dataObj.bar <= priceObject.bar) {
      dataNewObj.foo = dataObj.foo;
      dataNewObj.bar = dataObj.bar;
      dataNewObj.price = priceObject.price;
      break;
      }
      }
      dataNewArray.push(dataNewObj);
      });


      but it's solution is



      dataNewArray = [ 
      { foo: 0.25, bar: 1, price: 3 },
      { foo: 0.5, bar: 1, price: 3 },
      { foo: 1, bar: 1, price: 3 },
      { foo: 1, bar: 2, price: 3 },
      { foo: 2, bar: 1, price: 5 },
      { foo: 2, bar: 2, price: 5 },
      { foo: 2, bar: 4, price: 124 },
      { foo: 3, bar: 2, price: 3 },
      { foo: 4, bar: 1, price: 4 },
      { foo: 4, bar: 2, price: 4 },
      { foo: 4, bar: 4, price: 6 }
      ];


      So, you can see in this object { foo: 2, bar: 4, price: 124 }, price should be 7, not 124.



      How to solve my problem with help of anything js features like Lodash, or any algorithm. I tried many solutions at StackOverflow but maximum solutions for single attribute in object.










      share|improve this question













      I have this array of objects.



      var priceArray = [ 
      { "foo": 1, "bar": 2, "price": 3 },
      { "foo": 2, "bar": 123, "price": 124 },
      { "foo": 2, "bar": 2, "price": 5 },
      { "foo": 112, "bar": 2, "price": 75 },
      { "foo": 2, "bar": 3, "price": 5 },
      { "foo": 3, "bar": 2, "price": 3 },
      { "foo": 3, "bar": 4, "price": 7 },
      { "foo": 4, "bar": 3, "price": 4 },
      { "foo": 4, "bar": 4, "price": 6 }
      ];


      I have another array of objects.



      var dataArray = [ 
      { foo: 0.25, bar: 1 },
      { foo: 0.5, bar: 1 },
      { foo: 1, bar: 1 },
      { foo: 1, bar: 2 },
      { foo: 2, bar: 1 },
      { foo: 2, bar: 2 },
      { foo: 2, bar: 4 },
      { foo: 3, bar: 2 },
      { foo: 4, bar: 1 },
      { foo: 4, bar: 2 },
      { foo: 4, bar: 4 },
      ];


      I want to add price in dataArray which price row foo and bar values are closest greater in priceArray.



      for example - filtered data should like this



      var filterDataArray = [ 
      { foo: 0.25, bar: 1, price: 3 },
      { foo: 0.5, bar: 1, price: 3 },
      { foo: 1, bar: 1, price: 3 },
      { foo: 1, bar: 2, price: 3 },
      { foo: 2, bar: 1, price: 5 },
      { foo: 2, bar: 2, price: 5 },
      { foo: 2, bar: 4, price: 7 },
      { foo: 3, bar: 2, price: 3 },
      { foo: 4, bar: 1, price: 4 },
      { foo: 4, bar: 2, price: 4 },
      { foo: 4, bar: 4, price: 6 },
      ];


      If I sort priceArray with foo and bar value with help of these codes and then filter this array with dataArray. So, I didn't get my solution. like -



      priceArray = priceArray.sort(function(a, b) {
      return a.foo - b.foo || a.bar - b.bar;
      });
      or
      priceArray = _.sortBy(( _.sortBy(priceArray, 'bar')), 'foo');
      or
      priceArray = _.orderBy(priceArray, ['foo', 'bar'], ['asc', 'asc']);

      // create new data array with price
      const dataNewArray = ;
      dataArray.map(dataObj => {
      const dataNewObj = {};
      for(const priceObject of priceArray) {
      if (dataObj.foo <= priceObject.foo && dataObj.bar <= priceObject.bar) {
      dataNewObj.foo = dataObj.foo;
      dataNewObj.bar = dataObj.bar;
      dataNewObj.price = priceObject.price;
      break;
      }
      }
      dataNewArray.push(dataNewObj);
      });


      but it's solution is



      dataNewArray = [ 
      { foo: 0.25, bar: 1, price: 3 },
      { foo: 0.5, bar: 1, price: 3 },
      { foo: 1, bar: 1, price: 3 },
      { foo: 1, bar: 2, price: 3 },
      { foo: 2, bar: 1, price: 5 },
      { foo: 2, bar: 2, price: 5 },
      { foo: 2, bar: 4, price: 124 },
      { foo: 3, bar: 2, price: 3 },
      { foo: 4, bar: 1, price: 4 },
      { foo: 4, bar: 2, price: 4 },
      { foo: 4, bar: 4, price: 6 }
      ];


      So, you can see in this object { foo: 2, bar: 4, price: 124 }, price should be 7, not 124.



      How to solve my problem with help of anything js features like Lodash, or any algorithm. I tried many solutions at StackOverflow but maximum solutions for single attribute in object.







      javascript sorting lodash






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 8 at 8:10









      Arpit

      14618




      14618
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          You need to sort by the geometric distance of the items and pick the first one which match.






          var priceArray = [{ foo: 1, bar: 2, price: 3 }, { foo: 2, bar: 123, price: 124 }, { foo: 2, bar: 2, price: 5 }, { foo: 112, bar: 2, price: 75 }, { foo: 2, bar: 3, price: 5 }, { foo: 3, bar: 2, price: 3 }, { foo: 3, bar: 4, price: 7 }, { foo: 4, bar: 3, price: 4 }, { foo: 4, bar: 4, price: 6 }],
          dataArray = [{ foo: 0.25, bar: 1 }, { foo: 0.5, bar: 1 }, { foo: 1, bar: 1 }, { foo: 1, bar: 2 }, { foo: 2, bar: 1 }, { foo: 2, bar: 2 }, { foo: 2, bar: 4 }, { foo: 3, bar: 2 }, { foo: 4, bar: 1 }, { foo: 4, bar: 2 }, { foo: 4, bar: 4 }],
          result;

          priceArray.sort((a, b) =>
          (Math.pow(a.foo, 2) + Math.pow(a.bar, 2)) - (Math.pow(b.foo, 2) + Math.pow(b.bar, 2))
          );
          result = dataArray.map(({ foo, bar }) => ({
          foo,
          bar,
          price: priceArray.find(o => o.foo >= foo && o.bar >= bar).price
          }));

          console.log(result); // the result
          console.log(priceArray); // the sorted array

          .as-console-wrapper { max-height: 100% !important; top: 0; }








          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',
            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%2f53203657%2fget-closest-greater-value-in-multiple-attributes-of-array-of-objects%23new-answer', 'question_page');
            }
            );

            Post as a guest
































            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            3
            down vote



            accepted










            You need to sort by the geometric distance of the items and pick the first one which match.






            var priceArray = [{ foo: 1, bar: 2, price: 3 }, { foo: 2, bar: 123, price: 124 }, { foo: 2, bar: 2, price: 5 }, { foo: 112, bar: 2, price: 75 }, { foo: 2, bar: 3, price: 5 }, { foo: 3, bar: 2, price: 3 }, { foo: 3, bar: 4, price: 7 }, { foo: 4, bar: 3, price: 4 }, { foo: 4, bar: 4, price: 6 }],
            dataArray = [{ foo: 0.25, bar: 1 }, { foo: 0.5, bar: 1 }, { foo: 1, bar: 1 }, { foo: 1, bar: 2 }, { foo: 2, bar: 1 }, { foo: 2, bar: 2 }, { foo: 2, bar: 4 }, { foo: 3, bar: 2 }, { foo: 4, bar: 1 }, { foo: 4, bar: 2 }, { foo: 4, bar: 4 }],
            result;

            priceArray.sort((a, b) =>
            (Math.pow(a.foo, 2) + Math.pow(a.bar, 2)) - (Math.pow(b.foo, 2) + Math.pow(b.bar, 2))
            );
            result = dataArray.map(({ foo, bar }) => ({
            foo,
            bar,
            price: priceArray.find(o => o.foo >= foo && o.bar >= bar).price
            }));

            console.log(result); // the result
            console.log(priceArray); // the sorted array

            .as-console-wrapper { max-height: 100% !important; top: 0; }








            share|improve this answer

























              up vote
              3
              down vote



              accepted










              You need to sort by the geometric distance of the items and pick the first one which match.






              var priceArray = [{ foo: 1, bar: 2, price: 3 }, { foo: 2, bar: 123, price: 124 }, { foo: 2, bar: 2, price: 5 }, { foo: 112, bar: 2, price: 75 }, { foo: 2, bar: 3, price: 5 }, { foo: 3, bar: 2, price: 3 }, { foo: 3, bar: 4, price: 7 }, { foo: 4, bar: 3, price: 4 }, { foo: 4, bar: 4, price: 6 }],
              dataArray = [{ foo: 0.25, bar: 1 }, { foo: 0.5, bar: 1 }, { foo: 1, bar: 1 }, { foo: 1, bar: 2 }, { foo: 2, bar: 1 }, { foo: 2, bar: 2 }, { foo: 2, bar: 4 }, { foo: 3, bar: 2 }, { foo: 4, bar: 1 }, { foo: 4, bar: 2 }, { foo: 4, bar: 4 }],
              result;

              priceArray.sort((a, b) =>
              (Math.pow(a.foo, 2) + Math.pow(a.bar, 2)) - (Math.pow(b.foo, 2) + Math.pow(b.bar, 2))
              );
              result = dataArray.map(({ foo, bar }) => ({
              foo,
              bar,
              price: priceArray.find(o => o.foo >= foo && o.bar >= bar).price
              }));

              console.log(result); // the result
              console.log(priceArray); // the sorted array

              .as-console-wrapper { max-height: 100% !important; top: 0; }








              share|improve this answer























                up vote
                3
                down vote



                accepted







                up vote
                3
                down vote



                accepted






                You need to sort by the geometric distance of the items and pick the first one which match.






                var priceArray = [{ foo: 1, bar: 2, price: 3 }, { foo: 2, bar: 123, price: 124 }, { foo: 2, bar: 2, price: 5 }, { foo: 112, bar: 2, price: 75 }, { foo: 2, bar: 3, price: 5 }, { foo: 3, bar: 2, price: 3 }, { foo: 3, bar: 4, price: 7 }, { foo: 4, bar: 3, price: 4 }, { foo: 4, bar: 4, price: 6 }],
                dataArray = [{ foo: 0.25, bar: 1 }, { foo: 0.5, bar: 1 }, { foo: 1, bar: 1 }, { foo: 1, bar: 2 }, { foo: 2, bar: 1 }, { foo: 2, bar: 2 }, { foo: 2, bar: 4 }, { foo: 3, bar: 2 }, { foo: 4, bar: 1 }, { foo: 4, bar: 2 }, { foo: 4, bar: 4 }],
                result;

                priceArray.sort((a, b) =>
                (Math.pow(a.foo, 2) + Math.pow(a.bar, 2)) - (Math.pow(b.foo, 2) + Math.pow(b.bar, 2))
                );
                result = dataArray.map(({ foo, bar }) => ({
                foo,
                bar,
                price: priceArray.find(o => o.foo >= foo && o.bar >= bar).price
                }));

                console.log(result); // the result
                console.log(priceArray); // the sorted array

                .as-console-wrapper { max-height: 100% !important; top: 0; }








                share|improve this answer












                You need to sort by the geometric distance of the items and pick the first one which match.






                var priceArray = [{ foo: 1, bar: 2, price: 3 }, { foo: 2, bar: 123, price: 124 }, { foo: 2, bar: 2, price: 5 }, { foo: 112, bar: 2, price: 75 }, { foo: 2, bar: 3, price: 5 }, { foo: 3, bar: 2, price: 3 }, { foo: 3, bar: 4, price: 7 }, { foo: 4, bar: 3, price: 4 }, { foo: 4, bar: 4, price: 6 }],
                dataArray = [{ foo: 0.25, bar: 1 }, { foo: 0.5, bar: 1 }, { foo: 1, bar: 1 }, { foo: 1, bar: 2 }, { foo: 2, bar: 1 }, { foo: 2, bar: 2 }, { foo: 2, bar: 4 }, { foo: 3, bar: 2 }, { foo: 4, bar: 1 }, { foo: 4, bar: 2 }, { foo: 4, bar: 4 }],
                result;

                priceArray.sort((a, b) =>
                (Math.pow(a.foo, 2) + Math.pow(a.bar, 2)) - (Math.pow(b.foo, 2) + Math.pow(b.bar, 2))
                );
                result = dataArray.map(({ foo, bar }) => ({
                foo,
                bar,
                price: priceArray.find(o => o.foo >= foo && o.bar >= bar).price
                }));

                console.log(result); // the result
                console.log(priceArray); // the sorted array

                .as-console-wrapper { max-height: 100% !important; top: 0; }








                var priceArray = [{ foo: 1, bar: 2, price: 3 }, { foo: 2, bar: 123, price: 124 }, { foo: 2, bar: 2, price: 5 }, { foo: 112, bar: 2, price: 75 }, { foo: 2, bar: 3, price: 5 }, { foo: 3, bar: 2, price: 3 }, { foo: 3, bar: 4, price: 7 }, { foo: 4, bar: 3, price: 4 }, { foo: 4, bar: 4, price: 6 }],
                dataArray = [{ foo: 0.25, bar: 1 }, { foo: 0.5, bar: 1 }, { foo: 1, bar: 1 }, { foo: 1, bar: 2 }, { foo: 2, bar: 1 }, { foo: 2, bar: 2 }, { foo: 2, bar: 4 }, { foo: 3, bar: 2 }, { foo: 4, bar: 1 }, { foo: 4, bar: 2 }, { foo: 4, bar: 4 }],
                result;

                priceArray.sort((a, b) =>
                (Math.pow(a.foo, 2) + Math.pow(a.bar, 2)) - (Math.pow(b.foo, 2) + Math.pow(b.bar, 2))
                );
                result = dataArray.map(({ foo, bar }) => ({
                foo,
                bar,
                price: priceArray.find(o => o.foo >= foo && o.bar >= bar).price
                }));

                console.log(result); // the result
                console.log(priceArray); // the sorted array

                .as-console-wrapper { max-height: 100% !important; top: 0; }





                var priceArray = [{ foo: 1, bar: 2, price: 3 }, { foo: 2, bar: 123, price: 124 }, { foo: 2, bar: 2, price: 5 }, { foo: 112, bar: 2, price: 75 }, { foo: 2, bar: 3, price: 5 }, { foo: 3, bar: 2, price: 3 }, { foo: 3, bar: 4, price: 7 }, { foo: 4, bar: 3, price: 4 }, { foo: 4, bar: 4, price: 6 }],
                dataArray = [{ foo: 0.25, bar: 1 }, { foo: 0.5, bar: 1 }, { foo: 1, bar: 1 }, { foo: 1, bar: 2 }, { foo: 2, bar: 1 }, { foo: 2, bar: 2 }, { foo: 2, bar: 4 }, { foo: 3, bar: 2 }, { foo: 4, bar: 1 }, { foo: 4, bar: 2 }, { foo: 4, bar: 4 }],
                result;

                priceArray.sort((a, b) =>
                (Math.pow(a.foo, 2) + Math.pow(a.bar, 2)) - (Math.pow(b.foo, 2) + Math.pow(b.bar, 2))
                );
                result = dataArray.map(({ foo, bar }) => ({
                foo,
                bar,
                price: priceArray.find(o => o.foo >= foo && o.bar >= bar).price
                }));

                console.log(result); // the result
                console.log(priceArray); // the sorted array

                .as-console-wrapper { max-height: 100% !important; top: 0; }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 8 at 8:35









                Nina Scholz

                167k1382145




                167k1382145






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53203657%2fget-closest-greater-value-in-multiple-attributes-of-array-of-objects%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest




















































































                    Popular posts from this blog

                    Landwehr

                    Reims

                    Schenkenzell