Answer the question
In order to leave comments, you need to log in
How to set a key in an array (object) as a variable?
Hello. This is how PHP works. I need to do the same in JS:
$id = 'АЙДИ';
$key1 = 'ВНУТРЕННИЙ КЛЮЧ 1';
$key2 = 'ВНУТРЕННИЙ КЛЮЧ 2';
$val1 = 'ЗНАЧЕНИЕ1';
$val2 = 'ЗНАЧЕНИЕ2';
$id2 = 'АЙДИ2';
$key3 = 'ВНУТРЕННИЙ КЛЮЧ 3';
$key4 = 'ВНУТРЕННИЙ КЛЮЧ 4';
$val3 = 'ЗНАЧЕНИЕ3';
$val4 = 'ЗНАЧЕНИЕ4';
$array = [];
$array[$id][$key1] = $val1;
$array[$id][$key2] = $val2;
$array[$id2][$key3] = $val3;
$array[$id2][$key4] = $val4;
echo json_encode($array);
$array[$id][$key1] = $val4;
let id = 'АЙДИ';
let key1 = 'ВНУТРЕННИЙ КЛЮЧ 1';
let key2 = 'ВНУТРЕННИЙ КЛЮЧ 2';
let val1 = 'ЗНАЧЕНИЕ1';
let val2 = 'ЗНАЧЕНИЕ2';
let id2 = 'АЙДИ2';
let key3 = 'ВНУТРЕННИЙ КЛЮЧ 3';
let key4 = 'ВНУТРЕННИЙ КЛЮЧ 4';
let val3 = 'ЗНАЧЕНИЕ3';
let val4 = 'ЗНАЧЕНИЕ4';
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question