0

I want to compare the results I get back from filling out a form to the properties returned from json.

I am not sure about how to go over that.

This is what I have so far:

$('#btn').on('click', function() {
    let $inputs = $('#new_form :input');
    let new_vals = {};
    $inputs.each(function() {
        new_form[this.id] = $(this).val();
    });

    console.log(new_vals);
    $.getJSON(api, function(data) {
        data.forEach(d => {
            console.log(d.values);
        });
    });
});

My console.log() for new_vals: {start_date: "2019-12-25", end_date: "2020-04-15"}

my console.log() for d.values:

{start_date: "2020-01-01", end_date: "2020-03-15"}
{start_date: "2020-01-01", end_date: "2020-03-15"}
{start_date: "2019-12-25", end_date: "2020-04-15"}
{start_date: "2020-03-20", end_date: "2020-03-31"}
{start_date: "2019-10-01", end_date: "2020-03-31"}
{start_date: "2019-10-01", end_date: "2020-03-31"}
{start_date: "2020-01-01", end_date: "2020-01-31"}
{start_date: "2020-01-19", end_date: "2020-01-25"}

I want to compare both start_date and end_date to the properties in d.values and want to return the ones that match.

I want the 3rd value ({start_date: "2019-12-25", end_date: "2020-04-15"}) to be returned from the comparison above.

How do I do that?

nb_nb_nb
  • 1,243
  • 11
  • 36
  • 1
    Please try this d.values.filter(ob => ob.start_date === new_vals.start_date && ob.end_date === new_vals.end_date); – Harmandeep Singh Kalsi May 21 '20 at 14:23
  • There is no JSON in this question. Only objects and arrays of objects. JSON is a text format, like CSV. – Heretic Monkey May 28 '20 at 20:07
  • Does this answer your question? [How to determine equality for two JavaScript objects?](https://stackoverflow.com/questions/201183/how-to-determine-equality-for-two-javascript-objects) – Heretic Monkey May 28 '20 at 20:08
  • @HereticMonkey, Not really, and the d.values above are from the forEach – nb_nb_nb May 28 '20 at 20:10
  • I want to compare the values inside the getJSON and if they match, I want to pull up a modal with similar values, if not do something else – nb_nb_nb May 28 '20 at 20:10
  • The answers to the question linked describe how to compare two objects to determine if they are the same (exactly what your accepted answer's `isEqualObj` function does). – Heretic Monkey May 28 '20 at 20:14
  • I get that, I did that in https://stackoverflow.com/questions/62073555/if-compared-values-are-true-then-getjson?noredirect=1#comment109786373_62073555. The if statement in this question is returning results of both the if and the else. I was just wondering why that was heppening – nb_nb_nb May 28 '20 at 20:15

1 Answers1

2

You can simply check the two values inside your getJSON.

 $.getJSON(api, function (data) {
  data.forEach((d) => {
    if(d.values["start_date"] === new_vals["start_date"] && d.values["end_date"] === new_vals["end_date"]) {
      console.log(d.values);
    }
  });
 });

An alternative approach:

isEqualObj = (obj1, obj2) => {
    if(Object.keys(obj1).length !== Object.keys(obj2).length) return false;
    for(key of Object.keys(obj1)) {
        if(obj1[key] !== obj2[key]) return false;
    }
    return true;
}
 $.getJSON(api, function (data) {
  data.forEach((d) => {
    if(isEqualObj(d.values, new_vals)) {
      console.log(d.values);
    }
  });
 });
Sifat Haque
  • 5,357
  • 1
  • 16
  • 23