-3

I have the json data in array.


    var obj = [
            {
                student_data:{
                    name: 'brj',
                    id: '123',
                    marks:[{'hi': 12, 'mt': 20, 'en': 20}]
                }
            },
            {
                student_data:{
                    name: 'anand',
                    id: '124',
                    marks:[{'hi': 12, 'mt': 20, 'en': 20}]
                }
        
            }
        ]

here i want to replace student_data with their respective ids, and convert this array to json string. Also i would like to remove some of the fields like id which we are have used already in place of student_data. the output should look like


            

             {
                '123':{
                    name: 'brj',
                    
                    marks:[{'hi': 12, 'mt': 20, 'en': 20}]
                }
            },
            {
                '124':{
                    name: 'anand',
                    
                    marks:[{'hi': 12, 'mt': 20, 'en': 20}]
                }

            }

Somesh
  • 1
  • 1
  • 1
    it's not json, it's javascript object – Krzysztof Krzeszewski Jul 15 '21 at 10:22
  • yes i want to able to convert it to js objects. – Somesh Jul 15 '21 at 10:23
  • 1
    This is a duplicate of https://stackoverflow.com/questions/68381558/write-a-function-in-js-that-take-input-as-argument-and-returns-output – secan Jul 15 '21 at 10:23
  • What have you tried so far to solve this on your own (a second time)? – Andreas Jul 15 '21 at 10:25
  • yes i tried it so many times but unfortunately im not able to. – Somesh Jul 15 '21 at 10:25
  • @Somesh can you share some of the approaches you attempted, so that we can start from there? – secan Jul 15 '21 at 10:26
  • 1
    This doesn't justify to ask the same question again - without any (visible) further research attempts from you... – Andreas Jul 15 '21 at 10:26
  • 1
    Please read [What is the difference between JSON and Object Literal Notation?](https://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation). There is no JSON in your question. –  Jul 15 '21 at 10:41

6 Answers6

1

You could do something like this.

const obj = [
    {
        student_data:{
            name: 'brj',
            id: '123',
            marks:[{'hi': 12, 'mt': 20, 'en': 20}]
        }
    },
    {
        student_data:{
            name: 'anand',
            id: '124',
            marks:[{'hi': 12, 'mt': 20, 'en': 20}]
        }

    }
]

const result = obj.reduce( (result, item)=>{ 
    result[item['student_data'].id] = item;
    return result; 
} ,{} 
)

console.log("Array to Object :",result);
1

You can use object destructuring along with Array.map method as

const newObj = obj.map( (item ) => {
    const {student_data: {id, ...rest}} = item;
    return {[id]: rest}
})
0

With a usage of destructuring of objects and spread syntax

const obj = [{
    student_data: {
      name: 'brj',
      id: '123',
      marks: [{
        'hi': 12,
        'mt': 20,
        'en': 20
      }]
    }
  },
  {
    student_data: {
      name: 'anand',
      id: '124',
      marks: [{
        'hi': 12,
        'mt': 20,
        'en': 20
      }]
    }

  }
];
const result = obj.map(({student_data: {id, ...otherProperties}}) => ({[id]: otherProperties}));
console.log(result);
Krzysztof Krzeszewski
  • 5,912
  • 2
  • 17
  • 30
0

You can use Array.reduce and some Destructuring to create the desired object, like so:

const obj = [ { student_data:{ name: 'brj', id: '123', marks:[{'hi': 12, 'mt': 20, 'en': 20}] } }, { student_data:{ name: 'anand', id: '124', marks:[{'hi': 12, 'mt': 20, 'en': 20}] }  } ]

const result = obj.reduce((acc, { student_data: { id, name, marks } }) => { 
   if (!acc[id]) acc[id] =  { name, marks };
   return acc;
}, {})

console.log('Result:', result);
Terry Lennox
  • 29,471
  • 5
  • 28
  • 40
0

I wrote a function that does that for you. It takes array of objects as argument.

var obj = [{
    student_data: {
      name: 'brj',
      id: '123',
      marks: [{
        'hi': 12,
        'mt': 20,
        'en': 20
      }]
    }
  },
  {
    student_data: {
      name: 'anand',
      id: '124',
      marks: [{
        'hi': 12,
        'mt': 20,
        'en': 20
      }]
    }

  }
]

function rearrange(arr) {
  let n = {}

  for (var i = 0; i < arr.length; i++) {
    let s = {};

    let elem = arr[i].student_data;
    s[elem.id] = {
      name: elem.name,
      marks: elem.marks
    }
    n = Object.assign(n, s);
  }
  return n
}

console.log(rearrange(obj))
TechySharnav
  • 4,869
  • 2
  • 11
  • 29
0

Check this logic so you can create the dynamic key with the JSON object to make a custom object

Dynamic key to javascript object

  var obj = [
        {
            student_data:{
                name: 'brj',
                id: '123',
                marks:[{'hi': 12, 'mt': 20, 'en': 20}]
            }
        },
        {
            student_data:{
                name: 'anand',
                id: '124',
                marks:[{'hi': 12, 'mt': 20, 'en': 20}]
            }
    
        }
    ]
    var objArray = []

    obj.forEach(element => {

        let stData = element.student_data
        let objId = stData.id

        let objjj = {
            [objId] :  {
                name : stData.name,
                marks : stData.marks
            }
        }
        objArray.push(objjj)
    });
  • [This](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON) is the only JSON object in JavaScript. Please read [What is the difference between JSON and Object Literal Notation?](https://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation). –  Jul 15 '21 at 10:42