Many times you have an object and you want to clone or combine property of different objects to one object.
Let’s check how to do that with Object.assign().
The syntax of Object.assing is:
Object.assign(target, ...sources)
Suppose we have one object like the following:
let Employee = { name : 'Pietro', age : 44, Sex : 'male'};
We have also another object called EmployeeRecord which is as the following
let EmployeeRecord = {
name : 'Pietro', address : 'Colorado Avenue 45 Miami', seriority : 15, category_employee : 'Cat A'
};
Now we want to have a new object (t) which combines all properties of Employee and EmployeeRecord we can use the following command:
let t = Object.assign(Emnployee,EmployeeRecord)
Here is what will happen if we print to console the 3 objects above
As you can see if we print the new object “t” to console we can notice that this object has all the properties of the first object (Empployee) and also all properties of the second object (EmployeeRecord)
It is important to know that if one property has different value the property of the last object will overwrite the property of the first one.
We can verify that by changing slightly our previous example
let EmployeeRecord = {
name : 'Pietro2', address : 'Colorado Avenue 45 Miami', seriority : 15, category_employee : 'Cat A'
};
In this case the name property if different from the two objects.
If we re-run create a variable t and we assign both Employee and EmployeeRecprd
Please notice that now the name property is not Pietro but Pietro2.
In this post we will take a look at the difference between Pointers and Arrays.
Pointers and Arrays are not the same thing but there are a lot of common syntax that you can use
The biggest thing that they have in common is that both are sequence of memory address that you can access.
Let’s have a look at the array: you know that when you pass a variable by value to a function what happen is that the C Compiler makes a copy of the local variable in the stack and pass the copy to the function. Although both variables have the same value their memory address is different: in other words they are located in a different section of the memory.
Now when you pass one array as a value to a function the C compiler does not copy the whole array in the stack: that would be very inefficient. The C compiler will copy only the address of the first element.
That is a bit similar to what happen with a pointer where the C compiler move through the memory blocks starting from the first bloc.
Let’s have a look at an example. We declare an array of for elements and we fill with some integer values.
int myarr[4];
myarr[0]=20;
myarr[1]=40;
myarr[2]=60;
myarr[3]=80;
Now let’s print the memory location of the array. There are several way of doing that. We can do this way
printf("This is the memorry address of array using myarr %p\n",myarr);
Notice please: We have an integer array but we are asking to print the memory address with printf using the a format specifier p!:Format specifier p is used to “writes an implementation defined character sequence defining a pointer” (source https://www.programiz.com)
Now we can also print the memory address of the integer pointer using a different syntax
printf("This is the memorry address of array using &myarr %p\n",&myarr);
printf("This is the memorry address of array using &myarr[0] %p\n",&myarr[0]);
printf("This is the memorry address of array using &myarr[1] %p\n",&myarr[1]);
printf("This is the memorry address of array using &myarr[2] %p\n",&myarr[2]);
printf("This is the memorry address of array using &myarr[3] %p\n",&myarr[3]);
This will return the following values. Notice please: that if we do not pass the element of the array the Compiler (first printf statement) will return the address of the first element (this is similar to what happen with a pointer)
Now let’s suppose we want to have a pointer to the array
int *pointerArray;
pointerArray=myarr;
Let’s print the memory address of the pointer at different location
printf("\n");
printf("------How to move between memory address of the pointerArray---------:\n");
printf("This is the memorry address of the pointerArray base %p\n",pointerArray);
printf("This is the memorry address of the pointerArray+1 %p\n",pointerArray+1);
printf("This is the memorry address of the pointerArray+2 %p\n",pointerArray+2);
printf("This is the memorry address of the pointerArray+3 %p\n",pointerArray+3);
Those instruction will print all the memory blocs of the array
If I want to know the value of first element of array I can de-reference with the following standard command to de-reference the pointer
printf("This is the memorry address of the *(pointerArray+1) %d\n", *(pointerArray+1) );
But I can also use (notice please) the array’s way to de-reference the pointer
printf("This is the memorry address of the pointerArray[1] %d\n",pointerArray[1]);
The result will be exactly equal, in this example 40.
Once I have a pointer we can say that pointer is equal to an array. This expression is valid. However we cannot say that array is equal to pointer.
We also want to declare a variable type integer that we will use later.
int yourselection;
In our example we will give the user the option to select the programming language that is like the most.
printf("What do you like programming\n");
printf("0:Java\n");
printf("1:C\n");
printf("2:Python\n");
printf("3:Javascript\n");
printf("Please enter your selection:");
Now we ask the user to select one option:
printf("Please enter your selection:");
scanf("%d",&yourselection);
printf("%s\n",mylist[yourselection]);
In the code above we read the option provided by the user and then we print it out as shown in the figure below:
If you notice we call the array of pointer “mylist” and we pass the parameter that the user has selected.
Using array of pointers has also one benefit especially when dealing with strings: we can compare easily between 2 strings without need to check character by character the string nor we need to use the library function strcmp
Let’s view a practical application of that. Once the user selected is option the program will check if the user like C the most or not and print a different statement.
Here is the code:
if (mylist[yourselection]==mylist[1]) {
printf("You selected C, Congratulation\n");
}
else{
printf("You have selected %s", mylist[yourselection] );
printf(" which is not my favorite\n");
}
Here is what happen if we select Java for example
As you can see we were able to compare strings very easily without going character by character.