0

Hi all I have my JSON as follows

var autoNotifyCategories = {
"notify": [{
    "Category": "Category1",
    "Title": ["Title1", "Title2"],
    "Pending": "true"
  },
  {
    "Category": "Category2",
    "Title": ["Title1"],
    "Pending": "true"
  },
  {
    "Category": "Category3",
    "Title": ["Title1", "Title2", "Title3", "Title4"],
    "Pending": "true"
  },
]

}

I will have my filter as follows

var filter = {
    Category: 'Category3',
    Title: 'Title4'
};

At title I can pass any based on the requirement but what I need is I would like to filter exactly based on the parameters I passed and return the Pending value from the array. I tired as below but it is checking only for the Category it seems, I need to validate both category and title can some one help me, here is he fiddle I tried

// find elements
var banner = $("#banner-message")
var button = $("button")

// handle click and add class
button.on("click", function() {
  //banner.addClass("alt")
  var autoNotifyCategories = {
    "notify": [{
        "Category": "Category1",
        "Title": ["Title1", "Title2"],
        "Pending": "true"
      },
      {
        "Category": "Category2",
        "Title": ["Title1"],
        "Pending": "true"
      },
      {
        "Category": "Category3",
        "Title": ["Title1", "Title2", "Title3", "Title4"],
        "Pending": "true"
      },
    ]
  }
  var filter = {
    Category: 'Category3',
    Title: 'Title4'
  };
  //alert(autoNotifyCategories.notify);
  let s = autoNotifyCategories.notify.filter(function(item) {
    for (var key in filter) {
      if (item[key] === filter[key]) {
        return item.Pending;
      }

    }
  });
  alert(s[0].Pending);
});
body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
}

#banner-message {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  font-size: 25px;
  text-align: center;
  transition: all 0.2s;
  margin: 0 auto;
  width: 300px;
}

button {
  background: #0084ff;
  border: none;
  border-radius: 5px;
  padding: 8px 14px;
  font-size: 15px;
  color: #fff;
}

#banner-message.alt {
  background: #0084ff;
  color: #fff;
  margin-top: 40px;
  width: 200px;
}

#banner-message.alt button {
  background: #fff;
  color: #000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="banner-message">
  <p>Hello World</p>
  <button>Change color</button>
</div>

8
  • Please include relevant code directly into your question, not just as a link to an external service. Commented May 15, 2018 at 11:00
  • Your problem is that you are returning the item inside your loop over those filter properties, as soon as you find the first match. You need to replace this with a flag that gets modified accordingly inside the loop, and then return the item (or not) after the loop, based on that flag. Commented May 15, 2018 at 11:01
  • Some one has given this answer let filtered = autoNotifyCategories.notify.filter(e => e.Category === filter.Category && e.Title.includes(filter.Title)); which is working but I am getting an error as Property 'includes' does not exist on type 'string[]' Commented May 15, 2018 at 12:09
  • You can use indexOf instead to check whether the title contains a specific string. (Careful with what the return value of that method means.) Commented May 15, 2018 at 12:11
  • It is always returning value even if the condition doesn't matches Commented May 15, 2018 at 12:16

2 Answers 2

2

Given that the Title is an array you can use includes for validation.

var autoNotifyCategories = {
  "notify": [{
      "Category": "Category1",
      "Title": ["Title1", "Title2"],
      "Pending": "true"
    },
    {
      "Category": "Category2",
      "Title": ["Title1"],
      "Pending": "true"
    },
    {
      "Category": "Category3",
      "Title": ["Title1", "Title2", "Title3", "Title4"],
      "Pending": "true"
    },
  ]
}

var filter = {
  Category: 'Category3',
  Title: 'Title4'
};
var filtered = autoNotifyCategories.notify.filter((obj) => {
  return (obj.Category === filter["Category"]) && (obj.Title.includes(filter["Title"]))
})

alert(filtered[0].Pending)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Sign up to request clarification or add additional context in comments.

2 Comments

Hi I tried this but as said I am getting an error in type script as Property 'includes' does not exist on type 'string[]
Have you take a look at this: stackoverflow.com/questions/40545329/…
1

Please check your code written in JSfiddle.The array was not matched inittially. The array should be coverted to String for comparison.

Its working now..!

    let s = autoNotifyCategories.notify.filter(function(item) {
    let cnt  = 0,equal = 1;
    for (var key in filter) {
      cnt++;
      equal = (item[key].toString().includes(filter[key].toString()));
      if(equal && cnt == Object.keys(filter).length){
            return item[key];
      }
    }
  });

  if(s.length){
    alert(s[0].Pending)
  }else{
    alert('Not Found');
  }

JS Fiddle : https://jsfiddle.net/3f2Lmjyj/

6 Comments

No luck still the same
Replace === by includes .Its working jsfiddle.net/3s86f2kg/18
as said include is giving me the following error in type script Property 'includes' does not exist on type 'string[]'
Can we simplify this in one line, I tried with having ! symbol but didn't worked if (item[key].toString().includes(filter[key].toString())) { }else{ equal = 0; }
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.