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

7
  • Will there by only two arrays at the end jsonArr1 and jsonArr2 ? Commented Feb 23, 2018 at 10:31
  • Welcome to Stack Overflow! Please take the tour and read through the help center, in particular How do I ask a good question? Do your research, search 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 minimal reproducible example of your attempt and say specifically where you're stuck. People will be glad to help. Good luck! Commented Feb 23, 2018 at 10:32
  • 1
    That's not JSON. JSON is a textual notation for data exchange. (More here.) If you're dealing with JavaScript source code, and not dealing with a string, you're not dealing with JSON. Commented Feb 23, 2018 at 10:33
  • 1
    There is no such thing as a "json array" or "json object". JSON 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. Commented Feb 23, 2018 at 10:33
  • 1
    Then share the expected output which can hold dynamic number of output arrays. Commented Feb 23, 2018 at 10:34

3 Answers 3

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

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

4 Comments

Its the desired output. Thank you so much for the solution and the right direction :)
Instead of an object with "1", "2", "3" properties, why not an array of arrays?
@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 hmm, yes. I've got it. Thank you)
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);

Comments

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

Comments

Your Answer

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