0

I have a problem adding the object "myobj" to the arrays data / data2. As you see "myobj" a JS object which I would like to add to either data or data2. the functions are being triggered by clicks on different buttons.

console.log of myobj shows me

{ array: "arr_id_1", axis: "x", acc: "", vel: "", dist: "", jerk: "" }

I receive an error saying data2.splice() is not a function.

which is the format I need. "myobj" is supposed to be added to an array which I want to use JSON.stringify on. This JSON literal goes then to a python script via ajax. The array "data" is being filled with each click I perform correctly but not in the format for further processing. So I tried to fill array data2 since I have read that I could use .splice() as well. Unfortunately, console.log(data2) shows "undefined" for each field I try to fill and I have no idea how to solve it.

I tried to use JSON.stringify on "myobj" and as another attempt, I have tried to JSON.parse it back again. I tried adding the brackets and colons into quotes but no success either.

I am grateful for any advice or help.

var counterx = 0;

let data = [];
let data2 = {};

function valuesX() {
  counterx++;

  // does something here

  let arr_id = [];
  arr_id.name = 'arr_id_' + counterx;
  let ind = counterx - 1;
  let myobj;

  function arr() {

    var ind = sel.selectedIndex;
    var axis = sel.options[ind].text;
    arr_id.length = 0;
    arr_id.push(arr_id.name, axis, btna.value, btnv.value, btns.value, btnj.value)
    myobj = {
      array: arr_id[0],
      axis: arr_id[1],
      acc: arr_id[2],
      vel: arr_id[3],
      dist: arr_id[4],
      jerk: arr_id[5]
    };

    console.log(arr_id.name, arr_id)
    console.log(myobj)
    console.log(data.name, data)
    console.log(data2.name, data2)


  }
  data.name = 'data';
  data2.name = 'data2';
  data.splice(ind, 0, arr_id)
  data2.splice(ind, 0, myobj)
}
I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116
McMo
  • 151
  • 1
  • 9
  • do you get any errors in the console? – zfrisch Jan 19 '18 at 15:19
  • Yes. Sorry. I forgot to mention it. On `data2.splice{ind, 0, myobj}` i get data2.splice() is not a function. I will edit my post. – McMo Jan 19 '18 at 15:21
  • 1
    `let data = []; let data2 = {};` data2 is an object. You can't splice into objects, only arrays. – Shilly Jan 19 '18 at 15:21
  • @Shilly Thanks. makes sense. So the information I was given was simply incorrect. Any idea how I can do what I intent to? – McMo Jan 19 '18 at 15:25
  • Personally, I would just use push, not worry about the order of the objects and then use the array_id property to identify the object later on. – Shilly Jan 19 '18 at 15:29
  • `let data = []; data.push( { "array" : "arr_id_1", "axis" : "x", "acc" : null, "vel" : null, "dist" : null, "jerk" : null } );` Try looking for some more tutorials, since you seem to have quite a few 'quirks' in your code. – Shilly Jan 19 '18 at 15:34
  • @Shilly Thanks a lot. This is a really good idea. I will do. I am still learning but it is getting better bit by bit. – McMo Jan 19 '18 at 16:46

1 Answers1

0

SOLUTION:

I have converted the myobj to an object using Answer from JVE999, first suggestion and used .splice() to add the every new myobj to the array data. I have used splice because I need to overwrite already existing elements while keeping the order (Thus the indx) when I trigger the function (.splice() reference).

var arr_id = [];
arr_id.name = 'arr_id_'+counterx;
var myobj;
var indx = counterx-1;
var data_new;
data.name = 'data';

function arr(){

    var ind = sel.selectedIndex;
    var axis = sel.options[ind].text;

    arr_id.length = 0;
    arr_id.push(arr_id.name, axis, btna.value, btnv.value,btns.value, btnj.value)   
    myobj = {
            array:arr_id[0], 
            axis:arr_id[1], 
            acc:arr_id[2], 
            vel:arr_id[3], 
            dist:arr_id[4], 
            jerk:arr_id[5]
            };

    data_new = convArrToObj(myobj);
    data.splice(indx, 1, data_new)
    data_str = JSON.stringify(data)

    console.log(data.name, data)
    console.log(data_str)

    }
McMo
  • 151
  • 1
  • 9