Although an object is not an array, you still should be able to access it via brackets.
const contact = {
name: "Alphonse Fontaine"
}
const property = "name"
console.log(contact[property])
Or did I misunderstood?
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello,
I'm looking for an equivalent of this code.
$contact = 'name';
$name = $contact->$property;
Here the JS array of objects (generated by Laravel, perhaps there is something to do in the backend to have the right format ?).
[ { "id": 6, "name": "Alphonse Fontaine" }, { "id": 7, "name": "Véronique Courtois" } ]
This works only if I have an array with keys, but doesn't work with an object.
property = 'name';
name = contact[property];
But when I write this, it works.
property = 'name';
name = contact.name;
So I'd like to write something like this.
property = 'name';
name = contact.property;
Can you help me please ? ;)
Thanks a lot.
V
Although an object is not an array, you still should be able to access it via brackets.
const contact = {
name: "Alphonse Fontaine"
}
const property = "name"
console.log(contact[property])
Or did I misunderstood?
Please or to participate in this conversation.