Am facing an issue with while filtering array of object using JavaScript filter. Can any one help me to solve this issue.
Here is the code below.
var searchString = "item1";
var data = [
{
checkInfo: {
checkId: "item1"
}
},
{
checkInfo: {
checkId: "item2"
}
}
];
Am trying to filter above data using the below method.
const output = searchString ? data.filter(list => list.checkInfo.checkId === searchString)[0] : data[0];
I want to output if matched i want return matched object. If not matched always want to return first array of object from data array. It's working fine for match case. When searchString word not matching the ternary else condition not executing and it's returning undefined. Can any one look into my code and give your suggestion.
Am facing an issue with while filtering array of object using JavaScript filter. Can any one help me to solve this issue.
Here is the code below.
var searchString = "item1";
var data = [
{
checkInfo: {
checkId: "item1"
}
},
{
checkInfo: {
checkId: "item2"
}
}
];
Am trying to filter above data using the below method.
const output = searchString ? data.filter(list => list.checkInfo.checkId === searchString)[0] : data[0];
I want to output if matched i want return matched object. If not matched always want to return first array of object from data array. It's working fine for match case. When searchString word not matching the ternary else condition not executing and it's returning undefined. Can any one look into my code and give your suggestion.
Share Improve this question edited Aug 7, 2018 at 17:15 Mark 92.5k8 gold badges114 silver badges155 bronze badges asked Aug 7, 2018 at 17:14 vadlamanivadlamani 1372 gold badges2 silver badges10 bronze badges 2- it worked for me . when searchString is flasy it returns item1 – Stakvino Commented Aug 7, 2018 at 17:19
- @Stakvino that's what the OP says: "it's working fine for match case." – Mark Commented Aug 7, 2018 at 17:21
4 Answers
Reset to default 1You are currently checking if searchString
is defined and making the choice based on that. If you want to check if something matched with the filter you need to run the filter first and then check. Since it looks like you are only searching for the first match, you could just use find()
The following will check both:
var searchString = "item14";
var data = [{
checkInfo: {
checkId: "item1"
}
},
{
checkInfo: {
checkId: "item2"
}
}
];
let match = data.find(list => list.checkInfo.checkId === searchString)
const output = (searchString && match) || data[0];
console.log(output)
The filter
functions returns an empty array if nothing is found, which is a truth-y value, so the ternary condition never triggers the false condition.
Thus, the first entry in an empty array is undefined
.
Using the filter
we can do this way
var searchString = "item3";
var data = [
{
checkInfo: {
checkId: "item1"
}
},
{
checkInfo: {
checkId: "item2"
}
}
];
var temp = data.filter(item => (item.checkInfo.checkId === searchString));
temp = temp.length > 0 ? temp : data[0];
console.log(temp);
When you call the .find(callback)
method on an array, you will get back the first item that matches the condition that you specify. If no items are found, you will get back undefined
.