-4
{
  brandBrandId: {
    brandId: 2,
    brandName: "Hikka",
    isGun: true
  },
  modelId: "1",
  modelName: "Hikka model"
}, {
  brandBrandId: {
    brandId: 3,
    brandName: "Bebrand",
    isGun: true
  },
  modelId: "2",
  modelName: "Bebrand model 1"
}, {
  brandBrandId: {
    brandId: 2,
    brandName: "Hikka",
    isGun: true
  },
  modelId: "3",
  modelName: "Hikka model2"
}

javascript

var models = $scope.models;
console.log("selectedBrand---->" + selectedBrand);
    for (var i = 0; i < models.length; i++) {
    console.log(i+" : " +[i].brandBrandId.brandId)//ERROR HERE!

Error TypeError: Cannot read property 'brandId' of undefined

Question

How can I search models under the brand 2 with JavaScript? Like give me every model which has brandId=2. I was trying to for loop it, but I really can't do it and I hope that there is some better and wiser way to do it. I need every modelId and modelName under the specific brand. THANKS!

Solution

function getModels() {
                    var brandsmodels=new Array();
                     for (var i = 0; i < models.length; i++) {

                        if (models[i].brandBrandId.brandId === selectedBrand) {
                            brandsmodels.push({
                                "modelId": ""+models[i].modelId,
                                "modelName": ""+models[i].modelName,
                                "brandId": ""+selectedBrand
                            });

                        }
                    }
                    $scope.brandsmodels=brandsmodels;

                }
Sami
  • 2,311
  • 13
  • 46
  • 80
  • 2
    Your title says DOM, but in Your question there is nothing related to DOM. – taxicala Apr 13 '15 at 16:08
  • *"I hope that there is some better and wiser way to do it."* Not really. Please post what you tried. A loop is not *that* complicated. Maybe this helps: [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/q/11922383/218196). – Felix Kling Apr 13 '15 at 16:10
  • It is also tagged "arrays" and "for-loop", but those are not present either. – Mr. Polywhirl Apr 13 '15 at 16:10
  • Please, let us show the code you already tried! – F. Hauri - Give Up GitHub Apr 13 '15 at 16:10
  • @user4759923: This is *not* JSON. – Felix Kling Apr 13 '15 at 16:11
  • Sorry, my mistake :) I was trying to read answers and somehow my brains was still thinking DOM :) JSON I meant. – Sami Apr 13 '15 at 16:12
  • @Sami: This is not JSON. – Felix Kling Apr 13 '15 at 16:12
  • @Felix Kling: This is everything what my REST service is giving to me. @Produces({ "application/json"}) public List findAll() { return super.findAll(); } – Sami Apr 13 '15 at 16:24
  • Still, what you posted is not JSON. Even if you got the data as JSON, the issue is with accessing the parsed data, not with JSON. Also, what exactly do you expect `[i].brandBrandId` to do? – Felix Kling Apr 13 '15 at 16:32
  • @Felix Kling: I just want to see is my syntax working or not. If I check my "JSON" with developer tool, that is the syntax what it's showing to me. For instance [0].brandBrandId.brandId,[1].brandBrandId.brandId and [2].brandBrandId.brandId – Sami Apr 13 '15 at 16:39
  • 1
    `[i]` creates an array with a single element, the value of `i`. If you want to access the ith element of `models`, then you have to write `models[i]`, and then `models[i].brandBrandId.brandId` will work as well. Even if you are not very familiar with JS, this is how it works in many languages. [Learn more about arrays](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array). – Felix Kling Apr 13 '15 at 16:42
  • console.log(i+" : " +models[i].brandBrandId.brandId)--->TypeError: Cannot read property 'brandId' of undefined – Sami Apr 13 '15 at 16:55
  • Then the array seems to have an object/value that does not have a `brandBrandId` property. Not much we can do about it. – Felix Kling Apr 13 '15 at 17:45
  • @Felix Kling : I have no idea why it didn't work at first, but it did work later. Please check my solution part of my edited question. I hate javascript a lot :) Thanks for helping! – Sami Apr 13 '15 at 18:20

1 Answers1

0

You can use filter array method:

input.filter(function(model) {
   return model.brandBrandId.brandId == 2;
});
jcubic
  • 61,973
  • 54
  • 229
  • 402
  • I tried this: function getModels() { console.log("getModels()---->"); $scope.models = modelService.query(); models.filter(function(model) { return model.brandBrandId.brandId === 2; });//TypeError: Cannot read property 'brandId' of undefined – Sami Apr 13 '15 at 16:46