1

Maybe this isn't the right place to ask this but I need a advice since I'm stuck on this. I have this code:

$(document).ready(function(){
  var i = 0, j = 0, 
      manufacturerIds = [1,2,3,4,5,6,7],
      countryIds = [1,2,3,4,5,6,7,8,9,10],
      result = [];

  for (i; i < manufacturerIds.length; i++) {
    for (j; j < countryIds.length; j++) {
       result.push({
         i: {
             idMan: manufacturerIds[i],
             idCtr: [] // stuck on this point, don't know 
                       // where to go from here and don't know 
                       // if I'm doing things right
         }
       });
    }
  }   
});

And I'm trying to return a output like this:

[
  {
    "0": {
      "idMan": 1,
      "idCtr": [
        1,
        2,
        3,
        4,
        5
      ]
    },
    "1": {
      "idMan": 2,
      "idCtr": [
        1,
        2,
        3,
        4,
        5
      ]
    },
    "2": {
      "idMan": 3,
      "idCtr": [
        1,
        2,
        3,
        4,
        5
      ]
    }
  }
]

Can any give me some advice? Or help?

NOTE: I'm not sure this is the right or best way but I'm trying to build some kind of structure where I can differentiate each item on the object|array, why? Because I'll need to add new element to it. For example, this ouput will be valid too:

[
  {
    "0": {
      "idMan": 1,
      "idCtr": [
        1,
        2
      ]
    },
    "1": {
      "idMan": 1,
      "idCtr": [
        1,
        4,
        5
      ]
    },
    "2": {
      "idMan": 1,
      "idCtr": [
        3
      ]
    }
  }
]

Then having this I think will be easy to add new idCtr right? By accesing someVar[X].idCtr.push(newVal);. BTW, I write some var examples but the truth is those values are dynamic, just a start point for you to get the idea behind my doubt

ReynierPM
  • 17,594
  • 53
  • 193
  • 363
  • Does http://stackoverflow.com/questions/2295496/convert-array-to-json work for you? – Brian Hoover Dec 23 '14 at 16:46
  • Are you sure that's what you want? Its an array with one object in it? – charlietfl Dec 23 '14 at 16:49
  • By pushing i you will literally push the index "i", not the element i... – briosheje Dec 23 '14 at 16:50
  • @charlietfl and others take a look at the edit I made, perhaps this clear a bit what I'm trying to do – ReynierPM Dec 23 '14 at 16:58
  • you really don't need the outer array though, just one object – charlietfl Dec 23 '14 at 17:05
  • @charlietfl see my NOTE on the main post, having this scenario, and seeing your answer, how do I push new items on `idCtr` for a certain position? Lets said I want to add `6,7,8` to `idCtr` on "1", how you achieve this? – ReynierPM Dec 23 '14 at 17:08
  • 1
    `result[0][1].idCtr.push( someValue);` – charlietfl Dec 23 '14 at 17:10
  • @charlietfl how do I find for a item inside the object? Lets said I want to know if `"1": {"idMan": 1,"idCtr": [1,4,5]}` already exists and if exists just push the new `idCtr` without repeat them, otherwise just add them, how? – ReynierPM Dec 23 '14 at 17:49

2 Answers2

2

I believe this is more along the lines of what you are wanting

 var i = 0,
     j = 0,
     manufacturerIds = [1, 2, 3, 4, 5, 6, 7],
     countryIds = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
     result = [];

 for (i; i < manufacturerIds.length; i++) {
     /* create basic object*/
     var item = {};
     item[i] = {idMan: manufacturerIds[i],idCtr: []};
     /* now push country Ids*/
     for (var j=0;; j < countryIds.length; j++) {
         item[i].idCtr.push(countryIds[j]);
     }
      /* finished creating object*/
     result.push(item);
 }

DEMO

charlietfl
  • 170,828
  • 13
  • 121
  • 150
1

You can use JSON.stringify() to convert the result to JSON. There is another problem with i: {} try something like this:

$(document).ready(function(){
  var i = 0, j = 0, 
      manufacturerIds = [1,2,3,4,5,6,7],
      countryIds = [1,2,3,4,5,6,7,8,9,10],
      result = [];

  for (i; i < manufacturerIds.length; i++) {
    var obj = {};
    obj[i] = { idMan: manufacturerIds[i], idCtr: [] };

    for (j; j < countryIds.length; j++) {
       obj[i].idCtr.push(countryIds[j]);           
    }
    result.push(obj);
  }   
  var data = JSON.stringify(result);
});
Ragnar
  • 4,393
  • 1
  • 27
  • 40
  • I think he actually wants `obj[j]`, not `obj[i]`, else it will print this: `[{"0":{"idMan":1,"idCtr":[1,2,3,4,5]}},{"0":{"idMan":1,"idCtr":[1,2,3,4,5]}},{"0":{"idMan":1,"idCtr":[1,2,3,4,5]}},{"0":{"idMan":1,"idCtr":[1,2,3,4,5]}},{"0":{"idMan":1,"idCtr":[1,2,3,4,5]}},{"0":{"idMan":1,"idCtr":[1,2,3,4,5]}},{"0":{"idMan":1,"idCtr":[1,2,3,4,5]}},{"0":{"idMan":1,"idCtr":[1,2,3,4,5]}},{"0":{"idMan":1,"idCtr":[1,2,3,4,5]}},{"0":{"idMan":1,"idCtr":[1,2,3,4,5]}}]` – briosheje Dec 23 '14 at 16:53
  • @briosheje take a look to the NOTE I leave on the main post – ReynierPM Dec 23 '14 at 16:59
  • Take a look now, I think this will solve the problem – Ragnar Dec 23 '14 at 17:06