<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<div>
<h1>Copy array</h1>
<script>
$(document).ready(function () {
var array = [1, 2, 3, 4, 5];
var array1 = [6, 7, 8];
var arra2 = [1, 2, 3, 3, 4, 5]; var arra4 = [3, 3];
var arra3 = [3, 3, 5];
//remove all occurances of 3 from arra2
//output will be [1,2,4,5]
function unique(arra2) {
var result = [];
$.each(arra2, function (i, e) {
if ($.inArray(e, arra4) == -1) result.push(e);
});
return result;
}
console.log(unique(arra2));
//------------------------------------------------------------------//
//remove arra3 elements from arra2
//output like [1,2,4]
var newFileredArray = [];
arra2.filter(function (e, i) { //e= element i= index
if ($.inArray(e, arra3) == -1) {
newFileredArray.push(e);
}
return newFileredArray;
});
console.log(newFileredArray);
//------------------------------------------------------------------//
//remove 3 from array
var index = array.indexOf(3);
array.splice(index,1);
console.log(array);
//------------------------------------------------------------------//
//copy array by slice
var newArray = array.slice(0); //copy array by using slice, starting from 0
console.log(newArray);
//------------------------------------------------------------------//
//if i want to take [1,2,3] from original array
//can we take by using slice() method
var newArray1 = array.slice(0,3); //copy array by using slice, starting from 0
console.log(newArray1);
//------------------------------------------------------------------//
//copy array by using ...
var newArray1 = [...array]
console.log(newArray1);
//------------------------------------------------------------------//
//creating new object of same array. If change new object 0 index value to 10. It does not effect to original
//array 0 index value
var array1 = Object.assign({}, array);
array1[0] = 10;
console.log(array1[0]);
console.log(array[0]);
console.log(array1);
//------------------------------------------------------------------//
//combined result of one or more array
var combineArray = [...array, ...array1]
console.log(combineArray);
//------------------------------------------------------------------//
//object creation in javascript
var Person = { Id: 1, Name: 'Ashish', Address: 'Chandigarh' };
console.log(Person.Id + " " + Person.Name);
//------------------------------------------------------------------//
//another way of creating object in javascript
var obj = new Object();
obj.Id = 101;
obj.Name = "Aakash";
console.log(obj.Id + " " + obj.Name);
//------------------------------------------------------------------//
//deep copy is point to same reference copy of an object. If any property values is changed into new variable then original object
//properties values are also changed.
var deepCopy = Person;
deepCopy.Name = "Kumar Verma";
console.log(deepCopy.Id + " " + deepCopy.Name);
console.log(Person.Id + " " + Person.Name);
//------------------------------------------------------------------//
//shallow copy is created by using Object.Create() method. It will create new object with same property name and
//values but if changed any property values of newly created object then it does not effect on Original object
//properties values
var shallowCopy = Object.create(Person);
shallowCopy.Name = "Kumar Verma";
console.log(shallowCopy.Id + " " + shallowCopy.Name);
console.log(Person.Id + " " + Person.Name);
//------------------------------------------------------------------//
//Object.assign is used to combine both object values into new object
var persons = { Id: 101, Name: 'Radha' };
var person1 = { Name: 'Ashish Kumar Verma' };
var newObj = Object.assign({}, persons, person1);
console.log(newObj.Id + " " + newObj.Name);
});
</script>
</div>
No comments:
Post a Comment