📜  Lodash _.concat()函数

📅  最后修改于: 2022-05-13 01:56:22.274000             🧑  作者: Mango

Lodash _.concat()函数

Lodash被证明在处理数组、字符串、对象等时非常有用。它使数学运算和函数范式更容易、更简洁。 _.concat()函数用于连接 JavaScript 中的数组。

句法:

_.concat(array, [values])

参数:此函数接受上面提到的两个参数,如下所述:

  • 数组:它是一个要添加值的数组。
  • values:它是要添加到原始数组的值的数组。

注意:数组值也可以包含数组数组或要添加到原始数组的简单单个对象。

返回值:该函数返回串联后的数组。

为了更好地理解函数,下面给出了几个例子。

示例 1:

Javascript
// Requiring the lodash library
let lodash = require("lodash");
 
// Original array to be concatenated
let array = [1, 2, 3];
 
// Values to be added to original array
let values = [0, 5, "a", "b"]
 
let newArray = lodash.concat(array, values);
console.log("Before concat: " + array);
 
// Printing newArray
console.log("After concat: " + newArray);


JavaScript
// Requiring the lodash library
let lodash = require("lodash");
 
// Original array to be concatenated
let array = ["a", 2, 3];
 
// Array of array to be added
// to original array
let values = [0, 5, ["a", "b"]]
let newArray = lodash.concat(array, values);
console.log("Before concat: ", array);
 
// Printing array
console.log("After concat: ", newArray);


Javascript
// Requiring the lodash library
let lodash = require("lodash");
 
// Original array to be concatenated
let array = ["a", 2, 3];
 
// Object of values to be
// added to original array
let values = { "a": 1, "b": 2 }
let newArray = lodash.concat(array, values);
console.log("Before concat: ", array);
 
// Printing array
console.log("After concat: ", newArray);


输出:

示例 2:将数组数组添加到原始数组。

JavaScript

// Requiring the lodash library
let lodash = require("lodash");
 
// Original array to be concatenated
let array = ["a", 2, 3];
 
// Array of array to be added
// to original array
let values = [0, 5, ["a", "b"]]
let newArray = lodash.concat(array, values);
console.log("Before concat: ", array);
 
// Printing array
console.log("After concat: ", newArray);

输出:

示例 3:此示例将对象添加到数组中。

Javascript

// Requiring the lodash library
let lodash = require("lodash");
 
// Original array to be concatenated
let array = ["a", 2, 3];
 
// Object of values to be
// added to original array
let values = { "a": 1, "b": 2 }
let newArray = lodash.concat(array, values);
console.log("Before concat: ", array);
 
// Printing array
console.log("After concat: ", newArray);

输出: