js array and object

Posted by Zay on Sat, 05 Mar 2022 08:49:42 +0100

array

  1. What is an array?
  • The index starts from 0, and the collection composed of the same data type is called array.
  1. Why do you need arrays (what problems do arrays solve)?
  • For the unified management of the same type of data.
  1. What are the characteristics of arrays?
  • Index value;
  • The elements in the array are of the same data type;
  • It is ordered and has a length (the length of the array represents the number of elements)
  • Array elements can be any js data type

Write out array API with for loop

  • Add an element to the head of the array
//increase
//Add element to arr header
//Mode 1 value changing mode
var arr = [2, 6, 4, 7, 8, 10];
var name = "yyqx";
for (var i = arr.length; i >= 0; i--) {
  arr[i] = arr[i - 1]; //Change the element with index (i-1) to the position of the element with index I
}
arr[0] = name;
console.log(arr);
//Method 2: change the subscript of the original array
var arr = [2, 6, 4, 7, 8, 10];
var newArr = [];
var name = "yyqx";
for (var i = 0; i < arr.length; i++) {
  var count = i + 1;
  newArr[count] = arr[i];
}
newArr[0] = name;
console.log(newArr);
  • Add elements at the end of the array
//Add "dundun" to the tail
var arr = [2, 2, 3, 4, 6, 1];
//Ordinary way
arr[arr.length] = "dundun";
console.log(arr);
//Function encapsulation
function push(arr, name) {
  var arr1 = [];
  arr1[arr.length] = name;
  for (var i = 0; i < arr.length; i++) {
    arr1[i] = arr[i];
  }
  return arr1;
}
console.log(push(arr, "happy"));
//Add a new array at the end of the array
  • Adds an element at a specific location in the array
//Add the element 'dundun' in the third position of the array
var arr = [2, 5, 3, 4, 6];
var name = "dundun";
var old = [arr[3], arr[4]];
arr[3] = name;
for (var i = 0; i < old.length; i++) {
  var count = 3 + i + 1;
  arr[count] = old[i];
}
console.log(arr);

function

  • What is a function?
    With certain functions and repeated execution
  • Why are there functions? (what problems does the function solve)
    In order to solve the problems of on-demand code, regular code fragments, repeated writing and low maintainability
  • What are the functions
  1. Formal parameters: formal parameters can have n functions: accept arguments and save argument data, which is equivalent to variables
  2. Argument: it is specific data, which can be of any type js and passed in during function call
  3. return function: output the operation result inside the function to the outside of the function and end the statement inside the function

object

  • Object: used to represent data with different meanings
  • Object: key value pair
  • Keys are strings, equivalent to variables
  • value any type of js data

Function analog array API concat()

function concat() {
  //arguments
  //Yes: class array, API in data cannot be used
  //Function: accepts all arguments
  var res = [];
  console.log(arguments);
  //1: Get all arguments (array to be merged)
  for (var i = 0; i < arguments.length; i++) {
    //arr accepts every array
    var arr = arguments[i];
    //2: Combine the elements in the array and add them to res from the tail
    for (var j = 0; j < arr.length; j++) {
      res.push(arr[j]);
    }
  }
  return res;
}
var arr_ = concat([1, 2, 35, 6, 3], [134], [63, 4, 2, 5]);
console.log(arr_);

Exercises

  1. Change array to object
    Enter var arr = ['name', 'fanzhen', 'age', 12]
    Output: {name: 'fanzhen', age:12}
//Mode 1
var arr = ["name", "fanzhen", "age", 12];
var obj = {};
for (var i = 0; i < arr.length; i++) {
  if (i % 2 == 0) {
    key = arr[i];
  } else {
    obj[key] = arr[i];
  }
}
console.log(obj);
//Method 2: first put forward both key and value
var arr = ["name", "fanzhen", "age", 12];
var keys = [];
var vals = [];
for (var i = 0; i < arr.length; i++) {
  if (i % 2 == 0) {
    keys.push(arr[i]);
  } else {
    vals.push(arr[i]);
  }
}
var obj = {};
for (var j = 0; j < keys.length; j++) {
  obj[keys[j]] = vals[j];
}
console.log(obj);
  • Encapsulates a function that turns an array into an object
function arrObject(arr) {
  var obj = {};
  for (var i = 0; i < arr.length; i++) {
    if (i % 2 == 0) {
      key = arr[i];
    } else {
      obj[key] = arr[i];
    }
  }
  return obj;
}
console.log(arrObject(["name", "yyqx", "age", 21]));
  1. Filter fields with empty characters in the object
    Enter: {name: '', age:12}
    Output: {age:12}
//Mode 1
var obj = {name:'',age:12};
for(var key in obj){
   if(obj[key] == ''){
    delete obj[key];
} 
}
console.log(obj);
//Mode 2
var obj = {name:'',age:12};
var res = {};
for(var key in obj){
   if(obj[key] !== ''){
    res[key] = obj[key];
} 
}
console.log(res);
  • Encapsulates a function that filters the hollow characters of an object
function delEmpty(obj){
    for(var key in obj){
        if(obj[key] == ''){
            delete obj[key];
        }
    }
    return obj;
}
console.log(delEmpty({ name: "", age: 12 }))

Topics: Javascript Front-end