Xavier
Xavier

Reputation: 131

Browse json by value in javascript

{
    "brands": [
        {
            "name": "CarBrand1",
            "models": [
                {
                    "name": "CarModel1"
                },
                {
                    "name": "CarModel2"
                },
                {
                    "name": "CarModel3"
                }
            ]
        },
        {
            "name": "CarBrand2",
            "models": [
                {
                    "name": "CarModel1"
                },
                {
                    "name": "CarModel2"
                },
                {
                    "name": "CarModel3"
                }
            ]
        }
    ]
}

I've got a JSON like this and I would like to go inside CarBrand1 to get every car models of CarBrand1. I can do

json.brands[0]

to get CarBrand1 car models or

json.brands[1]

to get CarBrand2 car models. But what if I want to get CarBrand2 car models by the brand name value ("name": "CarBrand2") ?

Something like

json.brands['CarBrand1']

(but of course it's not the right way)

Upvotes: 0

Views: 58

Answers (4)

Rohìt Jíndal
Rohìt Jíndal

Reputation: 27202

Try this :

var jsonObj = {
    "brands": [
        {
            "name": "CarBrand1",
            "models": [
                {
                    "name": "CarModel1"
                },
                {
                    "name": "CarModel2"
                },
                {
                    "name": "CarModel3"
                }
            ]
        },
        {
            "name": "CarBrand2",
            "models": [
                {
                    "name": "CarModel1"
                },
                {
                    "name": "CarModel2"
                },
                {
                    "name": "CarModel3"
                }
            ]
        }
    ]
};

var res = jsonObj.brands.filter(obj => obj.name == 'CarBrand2');

console.log(res[0].models);

Upvotes: 0

A l w a y s S u n n y
A l w a y s S u n n y

Reputation: 38502

You can also do it with filter().

var json_data = {
  "brands": [{
      "name": "CarBrand1",
      "models": [{
          "name": "CarModel1"
        },
        {
          "name": "CarModel2"
        },
        {
          "name": "CarModel3"
        }
      ]
    },
    {
      "name": "CarBrand2",
      "models": [{
          "name": "CarModel1"
        },
        {
          "name": "CarModel2"
        },
        {
          "name": "CarModel3"
        }
      ]
    }
  ]
};

function getCarModelByName(name) {
  var json_array = json_data.brands;
  return json_array.filter(
    function(json_array) {
      if (json_array.name == name) {
        return json_array.models;
      }
    }
  );
}
var model = getCarModelByName('CarBrand2');
console.log(model[0].models)

Upvotes: 0

Derlin
Derlin

Reputation: 9881

There are no simple way to access objects by property names without iterating and comparing. I guess the simplest way is to use the find function:

json.brands.find((c) => c.name == "CarBrand1")

Note that if you often need to access the brands by name, I suggest you create a lookup dictionary of some sort. For example:

lookup = []; 
js.brands.forEach((c, i) => lookup[c.name] = c)

lookup["CarBrand1"] // --> {name: "CarBrand1", models: Array(4)}

Upvotes: 2

Ankit Agarwal
Ankit Agarwal

Reputation: 30739

You can create a reusable function that takes the car name as a parameter and return the car models array:

var car = {
    "brands": [
        {
            "name": "CarBrand1",
            "models": [
                {
                    "name": "CarModel1"
                },
                {
                    "name": "CarModel2"
                },
                {
                    "name": "CarModel3"
                }
            ]
        },
        {
            "name": "CarBrand2",
            "models": [
                {
                    "name": "CarModel1"
                },
                {
                    "name": "CarModel2"
                },
                {
                    "name": "CarModel3"
                }
            ]
        }
    ]
}
function getCarModel(name){
  for(var i=0; i<car.brands.length; i++){
    if(car.brands[i].name === name){
      return car.brands[i].models;
    }
  }
}

console.log(getCarModel("CarBrand2"));

Upvotes: 0

Related Questions