0

I have a json array like this:

    [
      {id:1, another_id:1},
      {id:2, another_id:1},
      {id:3, another_id:2}
    ]

Is it possible to divide this into json arrays based on the key another_id. In this case two json arrays should be created like this

  jsonArr1 = [
          {id:1, another_id:1},
          {id:2, another_id:1}
        ]

  jsonArr2 = [
          {id:3, another_id:2}
        ]

another_id will be varying. Please help guys

  • Will there by only two arrays at the end `jsonArr1` and `jsonArr2` ? – gurvinder372 Feb 23 '18 at 10:31
  • Welcome to Stack Overflow! Please take the [tour] and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) Do your research, [search](/help/searching) for related topics on SO, and give it a go. ***If*** you get stuck and can't get unstuck after doing more research and searching, post a [mcve] of your attempt and say specifically where you're stuck. People will be glad to help. Good luck! – T.J. Crowder Feb 23 '18 at 10:32
  • 1
    That's not JSON. JSON is a *textual notation* for data exchange. [(More here.)](http://stackoverflow.com/a/2904181/157247) If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. – T.J. Crowder Feb 23 '18 at 10:33
  • 1
    There is no such thing as a "json array" or "json object". [JSON](http://json.org) is a text representation of a data structure. It uses a subset of JavaScript and the code you posted is a JavaScript array that contains JavaScript objects. – axiac Feb 23 '18 at 10:33
  • Yes, parse the json into a real array and then either use `filter` or `reduce` to divide it into pieces. There's plenty of libraries that have this builtin, so first place to look is any library you use. Else you'll have to write a reduction yourself. – Shilly Feb 23 '18 at 10:33
  • @gurvinder372 No there will be multiple –  Feb 23 '18 at 10:33
  • 1
    Then share the expected output which can hold dynamic number of output arrays. – gurvinder372 Feb 23 '18 at 10:34

3 Answers3

3

If you do not know how many different result arrays you will have, you should not try to make a variable for each of them. Instead put them in an object, where each object property corresponds to a single possible value of another_id, and the value for it is the corresponding array.

You can achieve that with reduce:

var data = [{id:1, another_id:1},{id:2, another_id:1},{id:3, another_id:2}];

var result = data.reduce( (acc, obj) => {
    acc[obj.another_id] = acc[obj.another_id] || [];
    acc[obj.another_id].push(obj);
    return acc;
}, {});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
trincot
  • 317,000
  • 35
  • 244
  • 286
  • Its the desired output. Thank you so much for the solution and the right direction :) –  Feb 23 '18 at 10:56
  • Instead of an object with `"1", "2", "3"` properties, why not an array of arrays? – Mohammad Usman Feb 23 '18 at 11:02
  • @MohammadUsman, that would be an option, but then one must be aware that if the values of `another_id` are wide apart, you would be creating a sparse array. It also depends on whether you really need the output to have array features. This solution will also work if `another_id` is not numerical. – trincot Feb 23 '18 at 11:06
  • @trincot hmm, yes. I've got it. Thank you) – Mohammad Usman Feb 23 '18 at 11:10
1

If you want different variables then you can build a function which will return filtered array based on the passed value. This will use Array.filter()

function formatData(check) {
  var data = [{
      id: 1,
      another_id: 1
    },
    {
      id: 2,
      another_id: 1
    },
    {
      id: 3,
      another_id: 2
    }
  ];

  return data.filter(el => el.another_id === check);
}

jsonArr1 = formatData(1);
jsonArr2 = formatData(2);

console.log(jsonArr1);
console.log(jsonArr2);
void
  • 36,090
  • 8
  • 62
  • 107
0

I hope the code below will work for you. As this will create two separate json arrays Arr1 for id and Arr2 for another_id

data = [
      {id:1, another_id:1},
      {id:2, another_id:1},
      {id:3, another_id:2}
    ];
    console.log(data);
    var Arr1 = [];
    var Arr2 = [];
    for(var i in data){     
        Arr1.push(data[i].id);
        Arr2.push(data[i].another_id);
    }
Zainul Abideen
  • 1,829
  • 15
  • 37