-1

I have a for loop which basically goes through a list of categories. Each category (string) has 4 different items which can either be a true or a false. Below is a snippet of my code:

var i,
    category,
    items,
    categories= {};

    for (i = 0; i < categories.length; i++) {
        category = categories[i];

        items = {};
        items.first = availableItems[i][0] == true;
        items.second = availableItems[i][1] == true;
        items.third = availableItems[i][2] == true;
        items.fourth = availableItems[i][3] == true;

        categories+= { category : items};
    }

What i would like to end up is with a categories object structure like the below:

{ category1 : { 
               first : true,
               second : true,
               third : false,
               fourth : true
              },
  category2 : { 
               first : true,
               second : true,
               third : false,
               fourth : false
              },
 category3 : { 
               first : true,
               second : true,
               third : false,
               fourth : false
              }
}

Can anyone tell me what I am doing wrong?

Evan Davis
  • 35,493
  • 6
  • 50
  • 57
user1809790
  • 1,349
  • 5
  • 24
  • 53
  • 1
    `categories+= { category : items};` really? I'd suggest to use `categories` as _array_ and then use `push` to add objects in array. `var categories = [];` and inside `for` use `categories.push({ category : items});` – Tushar Oct 15 '15 at 15:47
  • @Tushar - I am new to JS, so if you can give me an example that would be great – user1809790 Oct 15 '15 at 15:47
  • @user1809790 there is an example in his comment. – Evan Davis Oct 15 '15 at 15:49
  • Could you please explain the question more? I had to delete my answer because I'm unsure what you're asking – Richard Hamilton Oct 15 '15 at 16:03
  • @RichardHamilton so basically i need to loop through a number of categories, and each category will have 4 items which can be either true or false each one of them. Now, I want to end up with an object that has the structure like { thisIsCategory1 : { first : true, second : false, third : true, fourth : false }, { thisIsCategory123 : { first : false, second : true, third : false , fourth : true } } As you can see, the category name is retrieved from the categories while looping – user1809790 Oct 15 '15 at 16:09

2 Answers2

1
var category = new Object;

for (i = 0; i < 2; i++) {
    category['category' + (i).toString()] =  (function() {
        var obj = {};
        obj.first = true == true;
        obj.second = false == true;
        return obj;
    })();  
};

Your code doesn't work because 1) You cannot get the lenght of an object with the .length property, so categories.length won't work. and 2) to add a member to an object you can use the bracket notation:

object['member'] = 'string value';

or the dot notation:

object.member = 'string value';

but not this:categories+= { category : items};

Community
  • 1
  • 1
Federico
  • 3,650
  • 4
  • 32
  • 45
1
var result;
for(var i=0;i<availableItems.length;i++){
   var index = i+1;
   result["category"+index]["first"] = availableItems[i][0] == true
   result["category"+index]["second"] = availableItems[i][1] == true
   result["category"+index]["third"] = availableItems[i][2] == true
   result["category"+index]["fourth"] = availableItems[i][3] == true
}
Jibin Mathew
  • 4,816
  • 4
  • 40
  • 68