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

8
  • Does stackoverflow.com/questions/2295496/convert-array-to-json work for you? Commented Dec 23, 2014 at 16:46
  • Are you sure that's what you want? Its an array with one object in it? Commented Dec 23, 2014 at 16:49
  • By pushing i you will literally push the index "i", not the element i... Commented Dec 23, 2014 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 Commented Dec 23, 2014 at 16:58
  • 1
    result[0][1].idCtr.push( someValue); Commented Dec 23, 2014 at 17:10

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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);
});

3 Comments

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 take a look to the NOTE I leave on the main post
Take a look now, I think this will solve the problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.