I have what I assume is an array in JavaScript that looks like this when I echo it to the console log using console.log(data.position);
c.Point {left: 98, right: 34, tmode: 15}
I am trying to get the values for left and right so I can use them elsewhere in the script. What can I try?
The console formatting of your object simply states that the object you're logging ( data.position
) is of class c.Point
, with properties left
, right
and tmode
.
To access these properties, just do it as if it's any other JavaScript object:
var left = data.position.left;
var right = data.position.right;
// example: do something with left and right
console.log(left, right); // prints '98 34'
or
// example: do something with data.position.left and data.position.right directly
console.log(data.position.left, data.position.right); // prints '98 34'
Es6 way using object.keys you can do this very easily
var boo ={left: 98, right: 34, tmode: 15};
var keys = Object.keys(boo);
console.log(keys);
//["left", "right", "tmode"]0: "left"1: "right"2: "tmode"length: 3__proto__: Array[0]
// now for getting specific keys using there index
let leftKey = keys[0];
let rightKey = keys[1];
console.log(leftKey); //get left key
console.log(rightKey); //get right key
Now in order to get the value of those keys do this,
console.log(boo[leftKey]); //98
console.log(boo[rightKey]) // 34
But as everyone mentioned that data.position is returning the object so, i believe you can access the value using data.position.KEYNAME
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.