0

i have this array obj:

var scenari =[
    {pos:'i4y',azione:('persa','avanti','indietro'),'peso':(0.3,0.4,0.3)},
    {pos:'g4r',azione:('persa','avanti','indietro'),'peso':(0.3,0.4,0.3)}
]; 

How to retrieve the array in key azione? i try this but is print only 'indietro' and not array

console.log (scenari[0]['azione']);//indietro
4
  • 5
    I would say, invalid json or object structure. Parenthesis on your "so called" array, are wrong. Commented Jun 10, 2017 at 6:35
  • 2
    You are using ( and ) instead of [ and ]. The key azione is not a JS array. Commented Jun 10, 2017 at 6:36
  • () are invalid. Need to be[] Commented Jun 10, 2017 at 6:37
  • The syntax is perfectly valid, the only problem is -- its not used to define an array. Commented Jun 10, 2017 at 6:40

3 Answers 3

1

Parentheses not define an array and must be use brackets ([]):

var scenari =[
    {pos:'i4y',azione:['persa','avanti','indietro'],'peso':[0.3,0.4,0.3]},
    {pos:'g4r',azione:['persa','avanti','indietro'],'peso':[0.3,0.4,0.3]}
]; 

console.log (scenari[0]['azione']);//indietro

Sign up to request clarification or add additional context in comments.

1 Comment

You could also select with console.log(scenari[0].azione); because it is an object property within an array.. :/
1

You are using () instead of [].

If you use () last value will be the value of key

var scenari =[
    {pos:'i4y',azione:['persa','avanti','indietro'],'peso':(0.3,0.4,0.3)},
    {pos:'g4r',azione:['persa','avanti','indietro'],'peso':(0.3,0.4,0.3)}
]; 

console.log (scenari[0]['azione']);

//If you use () 

//Example:


var ke = ('d','e');

console.log(ke);

Comments

0

You are getting this issue because tuple javascript is treating the data in () as a expression, so to get the result in the way you want you have to use [] or convert your data in string .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.