[Vuejs]-Javascript associate array gives many undefined fields

0👍

JavaScript does not have associative array like in PHP.

What JavaScript has that is most similar to associative array is an object. The difference is that an object is not iterable, while an associative array is iterable.

What you currently do is basically:

const data = [];  // [] is an array

data[10] = {
  item: 1
};

console.log(data);

What you need to do should be something like this:

const data = {};  // {} is an object

data[10] = {
  item: 1
};

console.log(data);

When you do json_encode() in PHP, it is actually converting associative array into a JSON, which is a valid JavaScript object, which do not support associative array as well.

So what it does would be something like this:

$data = [
  '10' => [
    'item' => 1
  ]
];

echo json_encode($data);

// output => { "10": { "item": 1 } }

Notice the {} syntax instead of [] syntax.

-2👍

array doesn’t have key, value. it just have value
if you use key, value data form, you should use object

let item = {104: item.info} 

how to use object :

item[104]

Leave a comment