How to add/remove properties to object in runtime in javascript [duplicate] - javascript

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"];

Related

Check condition for imbricated property in object [duplicate]

This question already has answers here:
Using optional chaining operator for object property access
(2 answers)
Access Javascript nested objects safely
(14 answers)
Closed 7 months ago.
I want to use the following condition
if (this.solvedConflictsDetails[this.instance.instanceId][this.model.name][this.entity.entityId].solved)
The problem is that if one of the above doesn't exist in the object it will say cannot read undefined
How would you check a condition like this?

Reference to variable versus "actual" variable (PHP vs Javascript) [duplicate]

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?

In Javascript, can I reference a key value pair from another key value pair? [duplicate]

This question already has answers here:
Self-references in object literals / initializers
(30 answers)
Can a JavaScript object property refer to another property of the same object? [duplicate]
(2 answers)
How do I reference the same Object's properties during its creation? [duplicate]
(5 answers)
How can a JavaScript object refer to values in itself? [duplicate]
(8 answers)
Can I reference other properties during object declaration in JavaScript? [duplicate]
(7 answers)
Closed 5 years ago.
var fighters = ['johnjones', 'rondarousey', 'connormcgregor', 'chuckliddel', 'demetriusjohnson'];
var warriors = {
wrestlers: ['randysavage', 'hulkhogan', 'ultimatewarrior', 'jakethesnake', 'milliondollarman'],
stable: [fighters, warriors.wrestlers]
}
I believe I can reference fighters from stable, but can I reference wrestlers from stable? In other words, how do I reference a key value pair from a later key value pair within the warriors object. Thank you for any and all help!
You cannot do that until the variable warriors is initialized. The only way is to wait and assign in the next line:
var warriors = {
wrestlers: ['randysavage', 'hulkhogan', 'ultimatewarrior', 'jakethesnake', 'milliondollarman'],
}
warriors.stable = [fighters, warriors.wrestlers]
Or use some kind of weird initialization like the one described here. Or use function constructor.

Sentence in javascript but variable [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 6 years ago.
i'm working with titanium and alloy.
I need run a JavaScript sentence like
$.D7F15.visible="true";
but D7F15 must be a variable, like
var mifranja="D7F15";
How i can run this??
some help please??
You can use bracket notation to access object property
var mifranja="D7F15";
$[mifranja].visible="true";

node-hashtable - how to get set of all keys? [duplicate]

This question already has answers here:
How to list the properties of a JavaScript object?
(18 answers)
Closed 8 years ago.
In Java it's called the keyset.
https://www.npmjs.org/package/node-hashtable
how come there is no method to return a list of all the keys?
Looking at the source there seems to be a method indexes which returna a list of the keys.

Categories

Resources