ES6 objects in the new method

1.object.is () determines whether the two values ​​are equal

  <script type="text/javascript">
            
        var flag1 = Object.is('foo', 'foo');
        console.log(flag1);//true
        
        var flag2 = Object.is('', '');
        console.log(flag2);//true

var flag3 = Object.is({}, {}); console.log(flag3);//false

var flag4 = Object.is(0, -0); console.log(flag4);//false var flag5 = Object.is(NaN, NaN);//true console.log(flag5) </script>

2.Object.assign () method for the combined object, the source object (source) of all enumerated attribute, copied to the target object (target).

Object.assignThe first parameter is the target object's method, the source object parameters are behind.

Note that if the source object and the target object has attributes with the same name, or a plurality of source objects of the same name attribute, after the attribute overwrite the previous attribute.

<script type="text/javascript">
            /*
             * object.asign() 
             */
            var target = {name:"小米"};
            var source1 = {
                age: 23,
                sex: 1,
                name: "xiaoming"
            };
            var source2 = {father:"大明"};
            
            console.log(Object.assign(target, source1, source2));//{name: "xiaoming", age: 23, sex: 1, father: "大明"} 
</script>

3.Object.keys(),Object.values(),Object.entries()

Object.keys method returns an array, the members of the object itself is a parameter (not inherited) all traverse (Enumerable) attribute keys.

var obj = { foo: 'bar', baz: 42 };
Object.keys(obj)
// ["foo", "baz"]

ES2017 introduced with the Object.keyssupport of Object.valuesand Object.entriesas a supplement to traverse an object, for for...ofrecycling.

let {keys, values, entries} = Object;
let obj = { a: 1, b: 2, c: 3 };

for (let key of keys(obj)) {
  console.log(key); // 'a', 'b', 'c'
}

for (let value of values(obj)) {
  console.log(value); // 1, 2, 3
}

for (let [key, value] of entries(obj)) {
  console.log([key, value]); // ['a', 1], ['b', 2], ['c', 3]
}

Object.valuesThe method returns an array, the members of the object itself is a parameter (not inherited) all traverse (Enumerable) key attribute.

const obj = { foo: 'bar', baz: 42 };
Object.values(obj)
// ["bar", 42]

Consistent with the rules described in section returns an array arrangement order of members, and "traversal attribute" chapter.

const obj = { 100: 'a', 2: 'b', 7: 'c' };
Object.values(obj)
// ["b", "c", "a"]

In the above code, called Attribute value is the value in accordance with the size, from small to large traversed, so that the sequence returns b, c, a.

Object.valuesOnly returns the object itself can traverse the property.

 

If the Object.valuesmethod parameter is a string, it returns an array of each character

Object.values('foo')
// ['f', 'o', 'o']

If the parameter is not an object, Object.valuesit will first turn its objects. Since the value of the object and packaging Boolean value, will not be added as an example of non-inherited properties. Therefore, Object.valuesit returns an empty array.

Object.values(42) // []
Object.values(true) // []

 

Object.entries()The method returns an array, the members of the object itself is a parameter (not inherited) all traverse (Enumerable) key attribute array.

const obj = { foo: 'bar', baz: 42 };
Object.entries(obj)
// [ ["foo", "bar"], ["baz", 42] ]

 

Object.entriesThe basic purpose is to traverse the object attributes.

let obj = { one: 1, two: 2 };
for (let [k, v] of Object.entries(obj)) {
  console.log(
    `${JSON.stringify(k)}: ${JSON.stringify(v)}`
  );
}
// "one": 1
// "two": 2

Object.entriesAnother useful method is that an object into a real Mapstructure.

const obj = { foo: 'bar', baz: 42 };
const map = new Map(Object.entries(obj));
map // Map { foo: "bar", baz: 42 }

Object.fromEntries()The method is Object.entries()a reverse operation for the object into a key array.

Object.fromEntries([
  ['foo', 'bar'],
  ['baz', 42]
])
// { foo: "bar", baz: 42 }
// 例一
const entries = new Map([
  ['foo', 'bar'],
  ['baz', 42]
]);

Object.fromEntries(entries)
// { foo: "bar", baz: 42 }

// 例二
const map = new Map().set('foo', true).set('bar', false);
Object.fromEntries(map)
// { foo: true, bar: false }

 

Guess you like

Origin www.cnblogs.com/150536FBB/p/11607658.html