1

This is my json object:

{
   "id": 2,
   "service": "mobile",
   "min": "20",
   "per": "10",
   "tax": "1",
   "categoryservices": [
       {
           "category": {
               "id": 1,
               "name": "laptop"
           }
       },
       {
           "category": {
               "id": 2,
               "name": "software"
           }
       }
   ]
}

I want my output like this:

{
   "id": 2,
   "service": "mobile",
   "min": "20",
   "per": "10",
   "tax": "1",
   "cats": [1,2] // this 1 and 2 is coming from categoriesservices array inside the category object i have id
}

How to do this using map function? I am new to javascript, which is good approach map or forloop?

7 Answers7

1

See destructuring assignment, Array.prototype.map(), and JSON for more info.

// Input.
const input = {
  "id": 2,
  "service": "mobile",
  "min": "20",
  "per": "10",
  "tax": "1",
  "categoryservices": [
    {
      "category": {
        "id": 1,
        "name": "laptop"
      }
    },
    {
      "category": {
         "id": 2,
         "name": "software"
      }
    }
  ]
}

// Categories => Objects to Cats => Ids.
const output = (input) => JSON.parse(JSON.stringify({
  ...input,
  cats: input.categoryservices.map(({category: {id}}) => id),
  categoryservices: undefined
}))

// Log.
console.log(output(input))
Arman Charan
  • 5,669
  • 2
  • 22
  • 32
0

If you are not worried about original object immutability, then try this

obj['cats'] = obj['categoryservices'].map(cat => cat.category.id);
delete obj['categoryservices'];
console.log(obj);
prabhatojha
  • 1,925
  • 17
  • 30
0

I just use .map() on categoryservices array:

    var output = {
   "id": 2,
   "service": "mobile",
   "min": "20",
   "per": "10",
   "tax": "1",
   "categoryservices": [
       {
           "category": {
               "id": 1,
               "name": "laptop"
           }
       },
       {
           "category": {
               "id": 2,
               "name": "software"
           }
       }
     ]
    };

    output.cats = output.categoryservices.map((element) => 
    element.category.id);
    delete output.categoryservices;
    console.log(JSON.stringify(output));
GrassLand
  • 5
  • 2
0

use .map() , it return value as array ! You want to change is categoryservices key only ! So delete that after you get wanted value ..

var output = {
   "id": 2,
   "service": "mobile",
   "min": "20",
   "per": "10",
   "tax": "1",
   "categoryservices": [
       {
           "category": {
               "id": 1,
               "name": "laptop"
           }
       },
       {
           "category": {
               "id": 2,
               "name": "software"
           }
       }
   ]
};
output.cats = output.categoryservices.map(i => i.category.id );
delete output.categoryservices;
console.log(output);
Jack jdeoel
  • 4,554
  • 5
  • 26
  • 52
  • i am using this code in my then fuction in server side ,so getting this error => " Cannot read property 'map' of undefined " –  Mar 14 '18 at 13:02
0

Try this working demo :

var jsonObj = {
   "id": 2,
   "service": "mobile",
   "min": "20",
   "per": "10",
   "tax": "1",
   "categoryservices": [
       {
           "category": {
               "id": 1,
               "name": "laptop"
           }
       },
       {
           "category": {
               "id": 2,
               "name": "software"
           }
       }
   ]
};

var arr = jsonObj.categoryservices.map(item => item.category.id)

jsonObj.cats = arr;
delete jsonObj.categoryservices;

console.log(jsonObj);
Debug Diva
  • 26,058
  • 13
  • 70
  • 123
0

Try this

var testData={
    "id": 2,
    "service": "mobile",
    "min": "20",
    "per": "10",
    "tax": "1",
    "categoryservices": [
        {
            "category": {
                "id": 1,
                "name": "laptop"
            }
        },
        {
            "category": {
                "id": 2,
                "name": "software"
            }
        }
    ]
}

testData.cats=[];

testData.categoryservices.forEach(function (item) {

    testData.cats.push(item.category.id);
});

delete testData.categoryservices;
console.log(testData);
-2

You can try using jquery each:

    <div id="log"></div>
        var conversation = {
      'John': {
        1: 'Test message 1',
        2: 'Test message 2',
        'Reply': {
          3: 'Test message 3',
          4: 'Test message 4'
        }
      },
      'Jack': {
        5: 'Test message 5',
        6: 'Test message 6'
      }
    };

    function iterate(obj) {
      if (typeof obj === 'string') {
        $('#log').append((obj + '<br/>'));
      }
      if (typeof obj === 'object') {
        $.each(obj, function(key, value) {
          iterate(value);
        });
      }
    }

iterate(conversation);
Sumanta736
  • 695
  • 3
  • 10