Hello good morning everyone, I hope you are well, I have the following JSON
'product': [
{
"Line_Type": "12345",
"Plan": "TRIO",
"Product": "TELEFONICA",
"Product_Type": "Secundario",
"Service": "00009",
"State": "ACTIVO",
"Technology": "NN"
},
{
"Line_Type": "123456",
"Plan": "TRIO",
"Product": "INTERNET",
"Product_Type": "Primario",
"Service": "000091",
"State": "ACTIVO",
"Technology": "NN"
}, {
"Line_Type": "123457",
"Plan": "TRIO",
"Product": "CABLE",
"Product_Type": "Primario",
"Service": "0000912",
"State": "ACTIVO",
"Technology": "NN"
},
]
And I am looking to go through that array in a function so that it only searches for "Product_Type"
and if it is == a, "Primario"
it returns the value "Product"
of that object.
I am doing it in the following way
var data = 'product': [
{
"Line_Type": "12345",
"Plan": "TRIO",
"Product": "TELEFONICA",
"Product_Type": "Secundario",
"Service": "00009",
"State": "ACTIVO",
"Technology": "NN"
},
{
"Line_Type": "123456",
"Plan": "TRIO",
"Product": "INTERNET",
"Product_Type": "Primario",
"Service": "000091",
"State": "ACTIVO",
"Technology": "NN"
}, {
"Line_Type": "123457",
"Plan": "TRIO",
"Product": "CABLE",
"Product_Type": "Primario",
"Service": "0000912",
"State": "ACTIVO",
"Technology": "NN"
},
]
function Buscador(){
switch (data){
case ('Product_Type' == "Primario") :
return data.product[0].Product || null
break;
}
};
But I'm not getting anything :/, I'd appreciate some help
I'm not sure how you look for the array to be traversed, if within a cycle, or that the function receives the array as a parameter and the function is in charge of traversing it, which is what would make the most sense; so that's what I did, create a function that receives an array as a parameter, and all it does is: with a conditional check the Product_Type property, and if it is "Primary"; in a new array it will push the Product property of that same object. I hope it helps you.
You can use the method
find
to search for matches within an array of objects, the find method will search for a single result.If you need to search for multiple results , I recommend you use the method
filter
, to get all the products of primary type in your array and it would look like this:Documentation filter
find documentation