0

Follow the example of my json in storage object: (sorry for the language)enter image description here

How can I put a name for the indexes "0:" and "1:" ? Example: Replace "0" for "session_0"

Below I show in a more abstract example how I create this array in code

var objectFinal = {"student":[],"sessions":[]};
var mySession = {"answers":[]}
objectFinal.sessions.push(mySession);
var obj = JSON.stringify(objectFinal);
localStorage.setItem("",obj);

I hope it has been clear, sorry for my bad english or any desorganization.

Dr.G
  • 448
  • 6
  • 19
  • 4
    If you give it a name, then it is not an array, it's an object of key-value pairs. – elclanrs Feb 07 '18 at 23:16
  • 2
    by the way, `mySession` is a javascript object, and `obj` is a string - there is no JSON array, as there is no such thing as a JSON array - unless of course you mean an array of JSON strings - which is nothing to do with the code you've presented – Jaromanda X Feb 07 '18 at 23:20
  • Arrays and objects are not really different : https://stackoverflow.com/a/47441358/1636522. –  Feb 07 '18 at 23:22
  • @leaf Well, an array is an object, sure. That answer says "Except for the differences between them, they are not different at all!", which is somewhat tautologous. – Heretic Monkey Feb 07 '18 at 23:31
  • @MikeMcCaughan You twist the reality a little bit, but I admit that this post might be hard to read for persnickety people that can't read between the lines. StackOverflow is not famous for its scientific papers right ? That being said, to clarify, with this post I just wanted to mitigate elclanrs statement. –  Feb 08 '18 at 06:58

3 Answers3

0

You can name the key using []

var objectFinal = {"student":[],"sessions":[]};
var mySession = {"answers":[]}
objectFinal.sessions['session_0'] = mySession; // <- here
var obj = JSON.stringify(objectFinal);
localStorage.setItem("",obj);
admcfajn
  • 2,013
  • 3
  • 24
  • 32
  • 1
    This actually adds the property `session_0` to the `sessions` array object. It will not be an element in the array (`objectFinal.sessions.length === 0`). – Heretic Monkey Feb 07 '18 at 23:22
  • 1
    That's right. To name the keys we need an object, not an array. As far as I know there's no way to name an array-key in javaScript. – admcfajn Feb 07 '18 at 23:29
0

You'll have to create an object to have custom keys. You can use Array.prototype.reduce on objectFinal.sessions, starting with an empty object {}:

var result = objectFinal.sessions.reduce(function(acc, current, i) {
    acc['session_' + i] = current;
    return acc;
}, {});
JJWesterkamp
  • 7,559
  • 1
  • 22
  • 28
0

Use the forEach(item, index) function to build the desired structure.

Look at this code snippet

var data = [{
    'datahora': '7758585',
    'nomeaplicador': 'Ele',
    'tempo': 000
  },
  {
    'datahora': '989887',
    'nomeaplicador': 'Enr',
    'tempo': 111
  }

];

data.forEach((e, index) => { 
    data[index] = { [`session_${index}`]: e };
});

console.log(data)
Ele
  • 33,468
  • 7
  • 37
  • 75