1

I have this Json. I want to use in AngularJs but I need modify this before. I tried to use foreach and reordering the objects but the final result is not enought good.

$scope.data = [
    {
        id: 1,
        company: "MOVISTAR",
        option: "PRECIO"
    },
    {
        id: 2,
        company: "MOVISTAR",
        option: "CALIDAD"
    },
    {
        id: 3,
        company: "MOVISTAR",
        option: "TECNOLOGIA"
    },
    {
        id: 4,
        company: "CLARO",       
        option: "PRECIO"
    },
    {
        id: 5,
        company: "CLARO",       
        option: "CALIDAD"
    };

I need format like this

$scope.datax = [

        {
            empresa: "MOVISTAR",
            description:[{
                id: 1,
                opcionResp: "PRECIO"
                },
                {
                id: 2,
                opcionResp: "CALIDAD"
                },
                {
                id: 3,
                opcionResp: "TECNOLOGIA"
                }]
        },
        {
            empresa: "CLARO",
            description:[{
                id: 4,
                opcionResp: "PRECIO"
                },
                {
                id: 5,
                opcionResp: "CALIDAD"
                }]
        }];

I don't have much experience with json and angular. Help me please.

Thanks so much

3
  • 2
    You'll probably want to take the time to learn JavaScript before diving into Angular. Here is a video on how to iterate through an array egghead.io/lessons/javascript-the-array-foreach-method . You'll need to use that to construct your "formatted" object Commented Jul 5, 2016 at 15:41
  • I can't think of any way through which you can convert the the JSON format. But why do you want to convert it to the other format? Angular will easily work with the format you have. You don't "NEED" to modify it. Commented Jul 5, 2016 at 15:43
  • Hi @someonenew . I've tried to make this example: plnkr.co/edit/D33ZECyE2dGOc6nynsQT?p=preview. It's the reason because I need format the Json. Commented Jul 7, 2016 at 8:32

3 Answers 3

2

Formatting json can be done by calling

angular.toJson(myObject)

To convert $scope.data to your desired structure, you can iterate $scope.data and build a new object as the following:

Online Demo - https://plnkr.co/edit/TFW38xWKhMxmTdf11vbQ?p=preview

function fixData(data) {

    var cache = {};
    var results = [];

    data.forEach(function(item) {
      var values = cache[item.company];

      if (!values) {
        values = [];

        results.push({
          empresa: item.company,
          description: values
        });

      }

      cache[item.company] = values;

      values.push({
        id: item.id,
        opcionResp: item.option
      })

    });

    return results;
}    

see online demo - https://plnkr.co/edit/TFW38xWKhMxmTdf11vbQ?p=preview

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

1 Comment

Thanks mate @jossefharush . It's I need
1

It´s quite simple with forEach:

var cache = {};

$scope.data.forEach(function(o){
  (cache[o.company] = (cache[o.company] || [])).push({id:o.id, opcionResp:o.option}) 
})

$scope.datax = Object.keys(cache).map(function(key){
    return {empresa:key,description:cache[key]}
})

Comments

0

First find all distinct companies, then go through the original data for each company and load up your description array:

        $scope.datax = []

        for (var i in $scope.data) {
            if (distinct.indexOf($scope.data[i].company) == -1) {
                $scope.datax.push({
                    empresa: $scope.data[i].company,
                    description: []
                })
            }
        }

        for (var j in $scope.datax) {
            for (var k in $scope.data) {
                if ($scope.datax[j].empresa == $scope.data[k].company) {
                    $scope.datax[j].description.push({
                        id: $scope.data[k].id,
                        opcionResp: $scope.data[k].option
                    })
                }
            }
        }

1 Comment

Always a pleasure @capitanjack

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.