JavaScript Unable to check empty validation inside the filter

I have tried empty validation using ternary operator, but it's not returning data. Below is my json object

var data = [ { "name": "test", "domain": "domain1", "subdomain": "subdomain1", "type": "yes" }, { "name": "hello", "domain": "domain2", "subdomain": "subdomain6", "type": "no" }, { "name": "test1", "domain": "domain2", "subdomain": "subdomain6", "type": "no", "desc":"helloo" } ];

Below is my filter js code

 var data = [{
		"name": "test",
		"domain": "domain1",
		"subdomain": "subdomain1",
		"type": "yes"
	},
    {
		"name": "hello",
		"domain": "domain2",
		"subdomain": "subdomain6",
		"type": "no"
	},
    {
		"name": "test1",
		"domain": "domain2",
		"subdomain": "subdomain6",
		"type": "no",
        "desc":"helloo"
	}
    
];

var namesearch = ["hello"];
var result = data.filter((d)=>{return 
namesearch.length > 0 ? namesearch.includes(d.name):''  });
console.log(JSON.stringify(result));
Answer:1

There is a small mistake in your code. You cannot use line break after the return keyword, javascript will return undefined in this case.

var data = [
  {
    name: "test",
    domain: "domain1",
    subdomain: "subdomain1",
    type: "yes"
  },
  {
    name: "hello",
    domain: "domain2",
    subdomain: "subdomain6",
    type: "no"
  },
  {
    name: "test1",
    domain: "domain2",
    subdomain: "subdomain6",
    type: "no",
    desc: "helloo"
  }
];

var namesearch = ["hello"];
var result = data.filter(d => {
  return namesearch.length > 0 ? namesearch.includes(d.name) : "";
});
console.log(JSON.stringify(result));
Answer:2

Is it possible to add eventListener with regex: Something like this: document.addEventListener(""+/\d+/+"", function (data) { }); Thanks!

Is it possible to add eventListener with regex: Something like this: document.addEventListener(""+/\d+/+"", function (data) { }); Thanks!

  1. addeventlistener regex

For example digits=4 means if input is 7 need output 7999, for 83 output should be 8399, for 9 output should be 9999. Want to add trailing 9 based on digits

For example digits=4 means if input is 7 need output 7999, for 83 output should be 8399, for 9 output should be 9999. Want to add trailing 9 based on digits

I would like to get a list of key names based on a value I check. This is what I have. const data = { "DE": "ARUN", "HI": "ARUN", "PR": "ARUN", "TX": "ARUN", "MA": "ARUN", "MD":...

I would like to get a list of key names based on a value I check. This is what I have. const data = { "DE": "ARUN", "HI": "ARUN", "PR": "ARUN", "TX": "ARUN", "MA": "ARUN", "MD":...

  1. excel list names based on criteria

I have a requirement to display additional input tags based on user radio tag. I pasted the working code here. I wanted to display spouse first name and last name input tags. If not, will have to ...

I have a requirement to display additional input tags based on user radio tag. I pasted the working code here. I wanted to display spouse first name and last name input tags. If not, will have to ...