This question already has answers here:
PHP foreach change original array values [duplicate]
(5 answers)
PHP Pass by reference in foreach [duplicate]
(9 answers)
Closed 4 years ago.
In the Javascript code:
var people = [{name:"john",age:20},{name:"bob",age:30},{name:"kate",age:40}];
people.forEach(function(person,i){
person.isHuman = true;
})
console.log(people) would give
[{name:"john",age:20,isHuman:true},{name:"bob",age:30,isHuman:true},{name:"kate",age:40,isHuman:true}]
However, the same code in PHP
$people = [["name"=>"john","age"=>20],["name"=>"bob","age"=>30],["name"=>"kate","age"=>40]];
foreach($people as $i=>$person){
$person['isHuman']=true;
}
var_dump($people) would give
[["name"=>"john","age"=>20],["name"=>"bob","age"=>30],["name"=>"kate","age"=>40]]
The new "isHuman" property is not added to the original array of objects.
In the PHP case, I know you can do "$people[$i]['isHuman']=true" to alter the object in the original array. But is there a way to do "$person['isHuman']=true" and have that change reflected in the original array?
And my second question is why/for what reasons is this behavior different between Javascript and PHP?
Related
This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
jQuery : how to access the name of the element [duplicate]
(3 answers)
Closed 1 year ago.
I have 2 variables.
var a = 1;
var b = 2;
Now I have 2 input boxes with name 'form-1-a' and 'form-1-b'.
I need to put variables in these input boxes. Please look at below code as it is not working.
$("input[name^='form-']").val(eval($(this).attr('name').split('-')[2]));
Though entire code is bit complex but simplified and it does not seem to work without any error message.
Javascript is fun and pain at same time.
Below code worked.
$("input[name^='form-']").each(function(){
$(this).val(eval($(this).attr('name').split('-')[2]))
});
This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 1 year ago.
let varone = "vartwo";
let vartwo = "answer";
And I would like to get "answer". But I cannot do the following:
console.log(vartwo);
How can I get "answer", but not by including "vartwo" in my code?
I think one way to resolve it, is by using javascript objects, as shown:
obj={
varone:"vartwo",
vartwo:"answer"
};
console.log(obj[varone]) //outputs "answer"
This question already has answers here:
Array.prototype.fill() with object passes reference and not new instance
(7 answers)
How to add same elements to javascript array n times
(4 answers)
Array.fill(Array) creates copies by references not by value [duplicate]
(3 answers)
Closed 3 years ago.
When I create an array of empty arrays with the following code:
const arrayOfArrays = new Array(3).fill([])
As expected, I get [[],[],[]. However let's say I change the value with:
arrayOfArrays[0][0] = 'foo'
I expect to get [['foo'],[],[]] but I end up with [['foo'],['foo'],['foo']]. Why is this the case? How can I get it so that it works as expected?
This question already has answers here:
Adding elements to object
(19 answers)
Closed 6 years ago.
I would like to know how to add/ remove properties to object at run time in javascript? How to achieve this in javascript ?
Let's say your object is myobj
then you can add a member like this
myobj.myvar = value; or myobj["myvar"] = value;
and remove it with
delete myobj.myvar; or delete myobj["myvar"];
This question already has answers here:
How do I enumerate the properties of a JavaScript object? [duplicate]
(14 answers)
Closed 8 years ago.
I am very new to javascript. When I do a typeof variable name, I get object{}. I need to look at the contents of this object. How would I do that in javascript?
You can use developer's console (Chrome console / Firebug / etc) or just convert your object to string:
var objAsString = JSON.stringify(yourVariable);