-1

I am building one nodejs application where in req.body:

I have this object:

{
   "id": 8,
   "username": "sameer",
   "age": "20"
   "details": [
       {
           "category": {
               "id": 1,
               "nickname": "sam"
           }
       },
       {
           "category": {
               "id": 2,
               "nickname": "basha"
           }
       }
   ]
}

My expected output:

 {
   "id": 8,
   "username": "sameer",
   "age" : "20",
   "final": [1,2]  // this id coming from category id.
  }

I tried this static way:

var data = 'myJsonStuffs'

var result = data.details.map(x => {
   return({
     "id": data.id,
     "username": data.username,
     "age": data.age,
     "final": [1,2] // i want this dynamic
   });
});

console.log(result);

How to do this using map? is this possible to return dynamic values.

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Mohamed Sameer
  • 2,998
  • 3
  • 22
  • 51

3 Answers3

2

You can use the function map:

Important: this approach will mutate the original obj.

var obj = {   "id": 8,   "username": "sameer",   "age": "20",   "details": [       {           "category": {               "id": 1,               "nickname": "sam"           }       },       {           "category": {               "id": 2,               "nickname": "basha"           }       }   ]};

obj.final = obj.details.map((d) => d.category.id);
delete obj.details

console.log(obj)
.as-console-wrapper { max-height: 100% !important; top: 0; }

Keeping the original obj structure

var obj = {   "id": 8,   "username": "sameer",   "age": "20",   "details": [       {           "category": {               "id": 1,               "nickname": "sam"           }       },       {           "category": {               "id": 2,               "nickname": "basha"           }       }   ]};

var newObj = Object.assign({}, { 
                id: obj.id, 
                username: obj.username, 
                age: obj.age, 
                final: obj.details.map((d) => d.category.id)
             });

console.log(newObj);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Ele
  • 33,468
  • 7
  • 37
  • 75
0

This is how I'd do it.

I hope this helps

const data = {
  id: 8,
  username: "sameer",
  age: "20",
  details: [
   {
     category: {
       id: 1,
       nickname: "sam"
     }
   },
   {
     category: {
       id: 2,
       nickname: "basha"
     }
   }
  ]
};

const result = Object.assign(
  {}, // Empty object to avoid mutating 'data'
  data,
  { details: data.details.map(detail => detail.category.id) } // Override 'details'
);

console.log(result);
3Dos
  • 3,210
  • 3
  • 24
  • 37
-1

Maybe you want this? you can use 'i' as index and populate arrays with that

var data = 'myJsonStuffs'

var result = data.details.map((x,i) => {
   return({
     "id": data.id,
     "username": data.username,
     "age": data.age,
     "final": [i] // i want this dynamic
   });
});

console.log(result);
Ricardo Costa
  • 704
  • 6
  • 27