Lodash _.zipObjectDeep() 方法
_.zipObjectDeep() 方法类似于 _.zipObject() 方法,不同之处在于它支持属性路径,并且它接受两个数组,一个属性标识符和一个对应值。
句法:
_.zipObjectDeep([props=[]], [values=[]])
参数:此方法接受上面提到的两个参数,如下所述:
- [props=[]](数组):此参数保存属性标识符。
- [values=[]](数组):此参数保存属性值。
返回值:此方法返回新对象。
示例一:这里使用 const _ = require('lodash') 来导入文件中的 lodash 库。
javascript
// Requiring the lodash library
const _ = require("lodash");
// Original array
var obj1 = ['a.b[0].c', 'a.b[1].d'];
// Use of _.zipObjectDeep() method
let gfg = _.zipObjectDeep(obj1, [1, 2]);
// Printing the output
console.log(gfg);
javascript
// Requiring the lodash library
const _ = require("lodash");
// Original array
var obj1 = ([ 40, 30, 90 ]);
// Use of _.zipObjectDeep() method
let gfg = _.zipObjectDeep(obj1, [ 1, 2, 3 ]);
// Printing the output
console.log(gfg);
javascript
// Requiring the lodash library
const _ = require("lodash");
// Original array
var obj1 = (['a', 'g', 'h',
'b', 'c', 'd', 'e', 'f']);
// Use of _.zipObjectDeep()
// method
let gfg = _.zipObjectDeep(obj1,
[1, 2, 3, 4, 5, 6, 7]);
// Printing the output
console.log(gfg);
输出:
{ a: { b: [ [ object ], [object] ] } }
示例 2:
javascript
// Requiring the lodash library
const _ = require("lodash");
// Original array
var obj1 = ([ 40, 30, 90 ]);
// Use of _.zipObjectDeep() method
let gfg = _.zipObjectDeep(obj1, [ 1, 2, 3 ]);
// Printing the output
console.log(gfg);
输出:
{ '30': 2, '40': 1, '90': 3 }
示例 3:
javascript
// Requiring the lodash library
const _ = require("lodash");
// Original array
var obj1 = (['a', 'g', 'h',
'b', 'c', 'd', 'e', 'f']);
// Use of _.zipObjectDeep()
// method
let gfg = _.zipObjectDeep(obj1,
[1, 2, 3, 4, 5, 6, 7]);
// Printing the output
console.log(gfg);
输出:
{ a: 1, g: 2, h: 3, b: 4, c: 5, d: 6, e: 7, f: undefined }
注意:此代码在普通 JavaScript 中不起作用,因为它需要安装库 lodash。