Operating Method of Javascript Object

Keywords: Attribute

1.for... in traversal object
The data in the object is transformed into an array.
for(var k in object name) {} traverses K as attribute name and object name [k] -> attribute value.

	var qq={
				name:'lihua',
				age:22,
				sex:'nv',
				play:function(){
					console.log('Can eat')
				}
			}
			for (var k in qq){
				console.log(k);// name age sex play
				console.log(k+'The value is:'+qq[k]);// K is the attribute name qq[k] attribute value
				console.log(k.length);// 43 34 Byte Length of Printed Attribute Values
			}

2. Object. keys (object name); returns the attribute name stored in the array;
The attributes of the object are traversed through the for loop.

	var obj01={
				name:'Xiao Ming',
				age:12,
				sex:'male'
			}
			var arr01=Object.keys(obj01); //Store the acquired attribute name in an array
			console.log(arr01);// (3) ["name", "age", "sex"];
			//Traverse the properties of obj objects through a for loop
			for (var i=0;i<arr01.length;i++) {
				console.log(arr01[i]);// name age sex
			}

3. Object. valus (object name); returns the attribute values stored in the array;
The attribute values in the object are traversed through the for loop.

	var obj02={
				play:'Play table tennis',
				like:'eat',
				weight:140
			}
			var arr02=Object.values(obj02);//Store the acquired attribute values in an array
			console.log(arr02);// (3) ["Playing table tennis", "eating", 140]
			//Traversing the attribute values of obj02 objects through a for loop
			for (var j=0;j<arr02.length;j++) {
				console.log(arr02[j]); // Playing table tennis 44 eat 140
			}

4. Object. assign (Object 1, Object 2... ) Object 1 is the object to be copied and object 2 is the object to be copied

Method only copies the source object's own enumerable attributes to the target object

	var obj03={a:123,b:'abc',c:'123'}
	var obj04={d:'qqq',e:'www',g:function(){return 'This is g Method'} }
	var obj06={d:'sss',f:'ooo'}
	var obj05= Object.assign(obj03,obj04);
	console.log(obj05);
	// {a: 123, b:'a B c', c:'one, two, three', d:'qqq', e:'www'}
	var obj07=Object.assign(obj03,obj04,obj05,obj06);
	// {a: 123, b:'a B c', c:'one, two, three', d:'qqq', e:'www',... }
	//When different objects contain the same attributes, the value of an object's attributes will be displayed.

5. Attribute delete attributes of deleted objects

	var obj08={name:'Little',age:11,sex:'male' }
	console.log(obj08) // {name:'little', age: 11, sex:'man'}
	delete obj08.name;
	console.log(obj08);// {age: 11, sex: male}

Posted by bdmovies on Thu, 03 Oct 2019 19:20:57 -0700