js array, object learning and other knowledge

Keywords: Javascript

Learning about array, object method, and array equality judgment

Array method


The forEach() method executes the given function once for each element of the array.

const array1 = ['a', 'b', 'c'];

array1.forEach(element => console.log(element));

// "a"
// "b"
// "c"

Use arr.forEach(callback(currentValue [, index [, array]])[, thisArg]).
A key:
1. If an arrow function expression is used to pass in a function parameter, the thisArg parameter will be ignored because the arrow function lexically binds the this value.
2. The return value is undefined.
3. The forEach() method executes the callback function once for each item in the array that contains valid values in ascending order, and those items that have been deleted or uninitialized will be skipped.

// three skipped.
//When an item containing the value "two" is reached, the first item of the entire array is removed, resulting in all remaining
// Move item up one position.Since the element "four" is positioned just before the array, "three" is skipped.   
// forEach() does not create a copy of the array before iteration.
var words = ['one', 'two', 'three', 'four'];
words.forEach(function(word) {
  if (word === 'two') {
// one
// two
// four

4. When forEach() is called, it does not change the original array, that is, the array to which it is called (the callback function may change the original array when called (that is, it can indirectly change the original array).

// Processing List Data
  @action dealEmployeeInfos(data) {
    data.forEach((item) => { // The original data will be modified
      const Obj = item;
      const {
        directSupervisor, numberOneLeader,
      } = item;
      const a = directSupervisor.users;
      const b = numberOneLeader.users;
      const directSupervisors = this.dealEmployeeInfoData(a);
      const numberOneLeaders = this.dealEmployeeInfoData(b);
      Obj.directSupervisors = directSupervisors;
      Obj.numberOneLeaders = numberOneLeaders;
      Obj.f = 1;
      item.directSupervisor = 1; // Errors cannot be modified directly
      console.log(Obj.f); // 1
      console.log(Obj); // Contains obj.f
      console.log(item); // Contains obj.f
    return data; // Modified data

5.forEach() executes the callback function once for each array element;Unlike map() or reduce(), it always returns an undefined value and is not chained.
6. There is no way to abort or jump out of the forEach() loop except to throw an exception.


The every() method tests whether all elements in an array can pass the test of a specified function.It returns a Boolean value.

const isBelowThreshold = (currentValue) => currentValue < 40;

const array1 = [1, 30, 39, 29, 10, 13];

console.log(array1.every(isBelowThreshold)); //  true

Use arr.every(callback(element[, index[, array]])[, thisArg])
Be careful:
every is similar to "all" in Mathematics in that it returns true when all elements are qualified.If an empty array is passed in, it will return true in any case.If a non-conformance is found, the every method will immediately return false.
callback is only called for indexes that have already been assigned.It will not be called for indexes that have been deleted or never assigned.

// Normal Edition
function isBigEnough(element, index, array) {
  return element >= 10;
[12, 5, 8, 130, 44].every(isBigEnough);   // false
[12, 54, 18, 130, 44].every(isBigEnough); // true
// Arrow Function Edition
[12, 5, 8, 130, 44].every(x => x >= 10); // false
[12, 54, 18, 130, 44].every(x => x >= 10); // true

2. Object Method


The hasOwnProperty() method returns a Boolean value indicating whether the object has a specified property (that is, whether there is a specified key) in its own property.

const object1 = {};
object1.property1 = 42;

// true

// false

// false

Meaning: All objects that inherit Objects will inherit to the hasOwnProperty method.This method can be used to detect whether an Object has specific properties of its own.The method ignores properties inherited from the prototype chain (the in operator returns true if the specified property is in the specified Object or its prototype chain).


The Object.keys() method returns an array of enumerable attributes of a given object in the order in which the attribute names are ordered as they would be returned during normal looping through the object.

var arr = ['a', 'b', 'c'];
console.log(Object.keys(arr)); // console: ['0', '1', '2']

// array like object
var obj = { 0: 'a', 1: 'b', 2: 'c' };
console.log(Object.keys(obj)); // console: ['0', '1', '2']

// array like object with random key ordering
var anObj = { 100: 'a', 2: 'b', 7: 'c' };
console.log(Object.keys(anObj)); // console: ['2', '7', '100']

// getFoo is a property which isn't enumerable
var myObj = Object.create({}, {
  getFoo: {
    value: function () { return this.foo; }
myObj.foo = 1;
console.log(Object.keys(myObj)); // console: ['foo']


1.Object.assign() method is used to assign the values of all enumerable properties from one or more source objects to the target object.It will return the target object.

const target = { a: 1, b: 2 };
const source = { b: 4, c: 5 };

const returnedTarget = Object.assign(target, source);

// Object { a: 1, b: 4, c: 5 }

// Object { a: 1, b: 4, c: 5 }

How to use: Object.assign(target,...sources)
2. If the attributes in the target object have the same key, the attributes will be overwritten by the attributes in the source object.The properties of subsequent source objects will similarly override those of previous source objects.

3.The Object.assign method only copies the source object's own enumerable attributes to the target object (shallow).

4. The method uses the source object [[Get]] and the target object [[Set]], so it calls the related getter s and setter s.So it assigns attributes, not just copying or defining new attributes

5. Attributes of String and Symbol types are copied.

// Copy properties of type symbol s
const o1 = { a: 1 };
const o2 = { [Symbol('foo')]: 2 };

const obj = Object.assign({}, o1, o2);
console.log(obj); // { a : 1, [Symbol("foo")]: 2 } (cf. bug 1207182 on Firefox)
Object.getOwnPropertySymbols(obj); // [Symbol(foo)]

6. In the event of an error, for example, if an attribute is not writable, a TypeError is raised, and if any attributes are added before the error is raised, the target object can be changed.Object.assign does not throw errors when those source object values are null or undefined.
7. Inheritable and non-enumerable properties cannot be copied.

// Inheritance and non-enumerable properties cannot be copied
const obj = Object.create({foo: 1}, { // foo is an inherited property.
    bar: {
        value: 2  // bar is a non-enumerable property.
    baz: {
        value: 3,
        enumerable: true  // baz is an enumerable property of its own.

const copy = Object.assign({}, obj);
console.log(copy); // { baz: 3 }

3. Methods of judging whether two arrays are equal in js

1. Convert the two arrays to a string comparison, but find that they are just not in the same order and are also considered not equal

[1,2,3].toString()== [3,2,1].toString() // false

2. We can sort the arrays in order from smallest to largest

[1,2,3].sort().toString()== [3,2,1].sort().toString() 
//true means that "1,2,3" = "1,2,3" results are equal

3. If the elements in the array are scalars and are not of type object, you can use==to compare the elements in the array:

scalarArrayEquals(array1,array2) {
return array1.length==array2.length &&
array1.every(function(v,i) { return v === array2[i]});

Posted by sfhc on Sun, 05 Sep 2021 09:08:24 -0700