If I have 2 type of objects:
object1 : {
value : { foo1: {}, foo2: 5 }, state: true, etc = {}
}
And
object2 : {
value : { foo1: { value: 5}, foo2: 6 }, state: true, etc = {}
}
If I do object1=object2 what exactly happens with object1 on all levels please.
I'm going to simplify that a bit:
var a = { value: 1, aStuff: true };
var b = { value: 2, bStuff: true };
b = a;
console.log(b); // { value: 1, aStuff: true }
Now a and b reference the same object. Think of it like the same object is accessible by two names. Which means this happens when you change that object:
a.value = 5
console.log(a); // { value: 5, aStuff: true }
Two names, one object.
So what happened to what to the { value: 2, bStuff: true } object? Once you tell b to reference a different object then no existing variable has a reference to it, so eventually the garbage collector will find it and dispose of it.
What happens with inner objects? That is the question..
Nothing at all. The outer object still holds references the values it contains. All that's changed is that you have two variables pointing to that same outer object.
object1 is now a reference of object2, any change in object1, will change object2;
var object1 = { foo: 'bar' };
var object2 = {
value : { foo1: { value: 5}, foo2: 6 }
};
object1 = object2; // the { foo: 'bar' } is gone.
object1.foo2 = 7; //This changes object2.foo2 value
console.log(object2.foo2); //7
Related
obj = {}
obj.a = function(a) {
console.log(a); // returns undefined
//console.log('Obj.a')
}
obj.b = function(a) {
console.log(a); // returns undefined
//console.log('Obj.b')
}
var node = {
a: {
use:'a',
node:[
{
create:'item',
description:{name:'Samsung'},
}
]
},
b: {
use:'b',
node:[
{
create:'item',
description:{name:'Apple'},
}
]
}
}
for(name in node) {
if(node.hasOwnProperty(name)) {
a = node[name];
// console.log(name)
obj[name].call(a)
}
}
In the above code, why do I get undefined when calling functions obj.a and obj.b from the loop below, instead of the object that's being passed to it?
function.call. you should pass the object instance as first param
According to the MDN
Note: In certain cases, thisArg may not be the actual value seen by the method.
If the method is a function in non-strict mode, null and undefined will be replaced with the global object, and primitive values will be converted to objects.
obj = {}
obj.a = function(a) {
console.log(a); // returns undefined
//console.log('Obj.a')
}
obj.b = function(a) {
console.log(a); // returns undefined
//console.log('Obj.b')
}
var node = {
a: {
use: 'a',
node: [{
create: 'item',
description: {
name: 'Samsung'
},
}]
},
b: {
use: 'b',
node: [{
create: 'item',
description: {
name: 'Apple'
},
}]
}
}
for (name in node) {
if (node.hasOwnProperty(name)) {
a = node[name];
// console.log(name)
obj[name].call(obj,a)
}
}
I'm currently transitioning from VueJS to AlpineJS and trying to update specific JSON arrays but it doesn't seem to be updating.
An example:
Any tips or reasonings why this is not working?
var foo = [
{ id: 0, title: 'test', text: c( this ) },
{ id: 1, title: 'test', text: 'text' },
{ id: 2, title: 'test', text: 'text' },
]
function c( idk ) {
console.log( idk.title )
}
console.log(foo)
var foo = (() => {
// Paste in your original object
const foo = [ {
a: 5,
b: 6,
}
];
// Use their properties
foo.c = foo.a + foo.b;
// Do whatever else you want
// Finally, return object
return foo;
})();
console.log( foo )
These examples were from Self-references in object literals / initializers but has been modified to use an Array of JSON
The first example doesn't work for exactly the same reason as the question you referenced.
The second example doesn't work because foo is the array and not the object inside the array.
foo.a + foo.b is going to be undefined + undefined which you then assign to foo.c making foo.c Not A Number.
You then console.log the array and it doesn't show the c property because it is an array and not designed to have named properties added to it.
var foo = (() => {
const foo = [{
a: 5,
b: 6,
}];
console.log(foo.a, foo.b);
foo.c = foo.a + foo.b;
console.log(foo.c);
return foo;
})();
console.log(foo.c)
I have a JSON object(mainObj) which in turn has objects (say obj1, obj2, obj3). What I am trying to achieve is when I check for a condition iterating through every obj in the mainObj and if it holds true, I want to add only the name of that obj in an array of String. Something like,
for(obj in mainObj){
if(obj holds condition){
add the descriptor of the obj (in string format) to an array (not the entire obj)
}
You can use Object.keys() to iterate over your object keys, then use Array.filter() to filter the keys, here I am checking if the inner objects have a property show and if this property is truthy:
const mainObj = {
obj1: { show: true, a: 1 },
obj2: { show: false, a: 2 },
obj3: { a: 3 },
obj4: { show: true, b: 1 }
};
const result = Object.keys(mainObj).filter(key => mainObj[key].show);
console.log(result);
If you want to use a for-in loop, you have to make sure the property is part of the object and is not inherited from its protype chain using Object.hasOwnProperty():
const mainObj = {
obj1: { show: true, a: 1 },
obj2: { show: false, a: 2 },
obj3: { a: 3 },
obj4: { show: true, b: 1 }
};
const result = [];
for (const prop in mainObj) {
if (mainObj.hasOwnProperty(prop) && mainObj[prop].show) {
result.push(prop);
}
}
console.log(result);
Hello I am struggling with arrays in JavaScript/NodeJS.
Basically, here is my code:
let arr = new Array();
arr = {
"Username" : var1,
"Console" : var2,
"Pseudo" : var3,
}
console.log(arr);
var1, 2 and 3 contains my data that is changing each time.
Let's consider for our first case:
var1 is "Johnson"
var2 is "ps4"
var3 is "Johnson46"
The code above would display the following:
{ Username: 'Johnson', Console: 'ps4', Pseudo: 'Johnson46' }
Now if the data in var1, var2, and var3 change, it will replace the array's content with new data. So if var1 = "Dave", var2 = "xbox" and var3 = "Dave78", it will replace the current array and display:
{ Username: 'Dave', Console: 'xbox', Pseudo: 'Dave78' }
But instead I would like my code to print the follwing:
{ Username: 'Johnson', Console: 'ps4', Pseudo: 'Johnson46' }
{ Username: 'Dave', Console: 'xbox', Pseudo: 'Dave78' }
See? Without overriding the array, just adding my data in succession. "line by line" or "step by step" if you prefer, sorry I don't really know how to say that.
You must push your objects to the array instead modifying it
let arr = new Array();
object1 = {
"Username" : "Jhon",
"Console" : "xbox",
"Pseudo" : "asd",
}
arr.push(object1)
object2 = {
"Username" : "Doe",
"Console" : "ps4",
"Pseudo" : "efg",
}
arr.push(object2)
console.log(arr);
I am going for more of a theoretical route. Maybe it will clear some of your doubt maybe it will not.
First of all, on a side note, Even though Array is technically an object but {..} is the notation for objects not arrays. If there is a object like {'0':.., '1':.., '2':..} and it has a length property then is called an array like object (thanks #Alnitak)
Now, when you are declaring an object, and adding values from variables if you're referencing an immutable object like string, number it will reference the value itself. If you're referencing say an object it will hold the object's reference and if you change the object it will change too.
For example:
const obj = {'a':'b'};
const x = {
m: obj.a
}
console.log(x);
obj.a = 'c';
console.log(x);
This will print,
{ m: 'b' }
{ m: 'b' }
But when your code is:
const obj = {'a':'b'};
const x = {
m: obj //referencing obj
}
console.log(x);
obj.a = 'c';
console.log(x);
It will print:
{ m: { a: 'b' } }
{ m: { a: 'c' } }
So, if you want your code to just print it then you can reference an object in your variable.
I am looking for a technique to run over a object of nested properties and wish to join the properties'.
This is the object I'd like to join:
var array = {
prop1: {
foo: function() {
// Your code here
}
},
prop2: {
bar1: 'some value',
bar2: 'some other value'
}
};
The result should look like this:
[
[ 'prop1', 'foo' ],
[ 'prop2', 'bar1' ],
[ 'prop2', 'bar2' ]
]
Then I'd like to join the array to strings formatted like this:
prop1.foo
prop2.bar1
prop2.bar2
Any tips?
EDIT: Forgot to say it should work for deeper arrays too.
Something along these lines? http://jsfiddle.net/X2X2b/
var array = {
prop1: {
foo: function() {
// Your code here
}
},
prop2: {
bar1: 'some value',
bar2: 'some other value'
}
};
var newA = [],
newB = [];
for ( var obj in array ) {
for (var inObj in array[obj]) {
newA.push([obj, inObj]);
newB.push(obj + '.' + inObj);
}
}
console.log(newA);
console.log(newB);
This is quite a different problem now that you have specified that it needs to support arbitrary depths. In order to solve it we need to use recursion and we need to use a second recursive parameter which keeps track of where we are in the nested hierarchy.
function objectPropertiesToArrays(obj, prepend) {
// result will store the final list of arrays
var result = [];
// test to see if this is a valid object (code defensively)
if(obj != null && obj.constructor === Object) {
for (var propertyName in obj) {
var property = obj[propertyName],
// clone prepend instantiate a new array
list = (prepend || []).slice(0);
// add the property name to the list
list.push(propertyName);
// if it isn't a nested object, we're done
if (property.constructor !== Object) {
result.push(list);
// if it is a nested object, recurse
} else {
// recurse and append the resulting arrays to our list
result = result.concat(objectPropertiesToArrays(property, list));
}
}
}
return result;
}
Example:
var obj = {
prop1: {
foo: function() { }
},
prop2: {
bar1: 'some value',
bar2: 'some other value'
},
prop3: {
x: {
y: [],
z: 'test'
},
erg: 'yar'
}
};
objectPropertiesToArrays(obj);
Returns
[
["prop1", "foo"],
["prop2", "bar1"],
["prop2", "bar2"],
["prop3", "x", "y"],
["prop3", "x", "z"],
["prop3", "erg"]
]