js/javascript operation object [Full] (including lodash of common operation objects)

catalogue

create object

Traversal object for in

Using lodash

Operation properties

Determine whether the specified attribute exists

get attribute

Add attribute

modify attribute

Delete attribute

Get the array keys() composed of attribute names [Lodash required]

Get the array values() composed of attribute values [Lodash required]

Batch modify attribute name [Lodash required]

Batch modify attribute value [Lodash required]

Get the attribute name that meets the condition [Lodash required]

Get the number of object attributes [Lodash required]

Object merge assign

[Lodash to be filtered]

Object inversion [Lodash required]

create object

Typically created using literal

let obj1 = {username: 'smyhvae', age: 26};

Traversal object for in

var obj = {
    name: "smyhvae",
    age: 26,
    gender: "male",
    address: "shenzhen"
};

//Enumerating properties in objects
for (var n in obj) {
    console.log("Attribute name:" + n);
    console.log("Attribute value:" + obj[n]); // Note that because the attribute name n here is a variable, if you want to obtain the attribute value, you can't write it as obj n. It's written as obj[n]
}

Using lodash

_. forInRight is reverse traversal

function Foo() {
  this.a = 1;
  this.b = 2;
}
 
Foo.prototype.c = 3;
 
_.forIn(new Foo, function(value, key) {
  console.log(key);
});
// =>Logs' a ',' B ', then' C '(the traversal order cannot be guaranteed).

Operation properties

When attribute names cannot be used Operator (such as 123, variable, etc.), you can only use [] to manipulate attributes

Determine whether the specified attribute exists

Attribute name in Object name

When using in to check whether an attribute is contained in an object, if it is not in the object but in the prototype, it will also return true.

Use the object's hasOwnProperty() to check whether the object itself contains the property.

Using lodash

  • _. has determines whether it is a direct attribute of an object
  • _. hasIn determines whether it is a direct or inherited property of an object.
var object = { 'a': { 'b': 2 } };
var other = _.create({ 'a': _.create({ 'b': 2 }) });
 
_.has(object, 'a');
// => true
 
_.has(object, 'a.b');
// => true
 
_.has(object, ['a', 'b']);
// => true
 
_.has(other, 'a');
// => false

get attribute

If you get a property that does not exist in the object, you will not report an error, but return undefined.  

 let name = Object name.Attribute name
// or
 let name = Object name[Attribute name]

Add attribute

Object name.Attribute name = Attribute value
// or
 Object name[Attribute name] = Attribute value

modify attribute

Object name.Attribute name = New attribute value 
// or
 Object name[Attribute name] = New attribute value 

Delete attribute

delete Object name.Attribute name
//or
delete Object name[Attribute name]

Get the array keys() composed of attribute names [Lodash required]

function Foo() {
  this.a = 1;
  this.b = 2;
}
 
Foo.prototype.c = 3;
 
_.keys(new Foo);
// => ['a', 'b'] (iteration order is not guaranteed)
 
_.keys('hi');
// => ['0', '1']

Get the array values() composed of attribute values [Lodash required]

function Foo() {
  this.a = 1;
  this.b = 2;
}
 
Foo.prototype.c = 3;
 
_.values(new Foo);
// =>[1, 2] (the traversal order cannot be guaranteed)
 
_.values('hi');
// => ['h', 'i']

Batch modify attribute name [Lodash required]

_.mapKeys({ 'a': 1, 'b': 2 }, function(value, key) {
  return key + value;
});
// => { 'a1': 1, 'b2': 2 }

Batch modify attribute value [Lodash required]

var users = {
  'fred':    { 'user': 'fred',    'age': 40 },
  'pebbles': { 'user': 'pebbles', 'age': 1 }
};
 
_.mapValues(users, function(o) { return o.age; });
// => { 'fred': 40, 'pebbles': 1 } (iteration order is not guaranteed)
 
// The `_.property` iteratee shorthand.
_.mapValues(users, 'age');
// => { 'fred': 40, 'pebbles': 1 } (iteration order is not guaranteed)

Get the attribute name that meets the condition [Lodash required]

_. findLastKey is a reverse traversal search

var users = {
  'barney':  { 'age': 36, 'active': true },
  'fred':    { 'age': 40, 'active': false },
  'pebbles': { 'age': 1,  'active': true }
};
 
_.findKey(users, function(o) { return o.age < 40; });
// => 'barney' (iteration order is not guaranteed)
 
// The `_.matches` iteratee shorthand.
_.findKey(users, { 'age': 1, 'active': true });
// => 'pebbles'
 
// The `_.matchesProperty` iteratee shorthand.
_.findKey(users, ['active', false]);
// => 'fred'
 
// The `_.property` iteratee shorthand.
_.findKey(users, 'active');
// => 'barney'

Get the number of object attributes [Lodash required]

_.size({ 'a': 1, 'b': 2 });
// => 2

Object merge assign

Object. Assign (target object, source object 1, source object 2...)

Function: merge multiple objects into a new object (append the attribute of the source object to the target object. If the attribute name in the object is the same, it will be overwritten.)

        let obj1 = { name: 'smyhvae', age: 26 };
        let obj2 = { city: 'shenzhen' };
        let obj3 = {};

        Object.assign(obj3, obj1, obj2); // Copy the properties of obj1 and obj2 to obb3

Object filtering [Lodash required]

Selected attribute https://www.lodashjs.com/docs/lodash.pick

var object = { 'a': 1, 'b': '2', 'c': 3 };
 
_.pick(object, ['a', 'c']);
// => { 'a': 1, 'c': 3 }

Exclude attributes https://www.lodashjs.com/docs/lodash.omit

var object = { 'a': 1, 'b': '2', 'c': 3 };
 
_.omit(object, ['a', 'c']);
// => { 'b': '2' }

Object inversion [Lodash required]

The attribute value changes to the attribute name, and the attribute name changes to the attribute value

var object = { 'a': 1, 'b': 2, 'c': 1 };
 
_.invert(object);
// => { '1': 'c', '2': 'b' }

 

Added by sivarts on Wed, 09 Feb 2022 13:24:49 +0200