I've below Json object that contain car associated to their manufacturer. From Json object, I want to get total number of cars as well as occurrence of a specific car in the object.
var Cars = { "manufacturer":"Car":
[{"Saab":"Automobile AB"},
{"Volvo":"V40"},
{"BMW":"Estoril Blue"},
{"Volvo":"V40"},
]};
I tried to use the filter but since filter is only specific for Arrays so cannot used it with Json object. Below is the source code.
var Cars = {"manufacturer":"Car":
[{"Saab":"Automobile AB"},
{"Volvo":"V40"},
{"BMW":"Estoril Blue"},
{"Volvo":"V40"},
]};
var volvo = "V40";
var numberOfCars = Cars.filter(function (x) {
return x === volvo;
}).length;
I expect the output of the above source code as 2. But I get an exception
Cars.filter is not a function
I need you guys to please help me to get the occurrence of V40 (that is 2), as well as total number of cars (which are 4).