OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Using a variable inside includes in filter in javascript

  • Thread starter Thread starter Vanshika Chaturvedi
  • Start date Start date
V

Vanshika Chaturvedi

Guest
I have a capm application which is using a value help and the following event is written for that.

Code:
    this.on('READ', 'Entity', async (req, res) => {
        var data = [
            {
                name: "Test",
                description: "test desc"
            },
            {
                name: "Test1",
                description: "Test1 desc"
            },
            {
                name: "Test2",
                description: "Test2 desc"
            }];

        const name = "name";
        console.log(req._query?.$search);
        var val = '';
        val = req._query?.$search;
        if (req._query?.$search && data.length > 0) {
            vr = await filterValueHelp(data, val);
            console.log("vr", vr);
            //console.log(data);
        }
        return data;
    });

I am trying to apply filter on this using the filterValueHelp function which is as such

Code:
 async function filterValueHelp(data, search) {
            console.log(typeof(search));
            console.log(typeof(data[0].name));
            
            let d = data.filter( obj => {
                let nameMatches = obj.name.includes(search);    
                return nameMatches;        
            });
            console.log(d);
    }

In the return I am getting an empty array in vr

Please help me with this how can I filter based on name.

I am expecting filtered array based on name.

<p>I have a capm application which is using a value help and the following event is written for that.</p>
<pre><code> this.on('READ', 'Entity', async (req, res) => {
var data = [
{
name: "Test",
description: "test desc"
},
{
name: "Test1",
description: "Test1 desc"
},
{
name: "Test2",
description: "Test2 desc"
}];

const name = "name";
console.log(req._query?.$search);
var val = '';
val = req._query?.$search;
if (req._query?.$search && data.length > 0) {
vr = await filterValueHelp(data, val);
console.log("vr", vr);
//console.log(data);
}
return data;
});
</code></pre>
<p>I am trying to apply filter on this using the filterValueHelp function which is as such</p>
<pre><code> async function filterValueHelp(data, search) {
console.log(typeof(search));
console.log(typeof(data[0].name));

let d = data.filter( obj => {
let nameMatches = obj.name.includes(search);
return nameMatches;
});
console.log(d);
}
</code></pre>
<p>In the return I am getting an empty array in <strong>vr</strong></p>
<p>Please help me with this how can I filter based on name.</p>
<p>I am expecting filtered array based on name.</p>
 

Latest posts

Top