当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


Collect.js put()用法及代码示例

put()方法用于设置集合中的给定键和值。

用法:

collect(array).put(key)

参数:collect()方法采用一个参数,该参数将转换为集合,然后在其上应用put()方法。 put()方法将 key 保留为参数。

下面的示例说明了collect.js中的put()方法:

范例1:



Javascript

const collect = require('collect.js'); 
  
let obj = ['Geeks', 'GFG', 'GeeksforGeeks']; 
  
const collection = collect(obj); 
  
collection.put('Welcome'); 
  
console.log(collection.all());

输出:

[ 'Geeks', 'GFG', 'GeeksforGeeks', Welcome:undefined ]

范例2:

Javascript

onst collect = require('collect.js'); 
  
let obj = { 
    name:'Rahul', 
    dob:'25-10-96', 
    section:'A', 
    score:98, 
}; 
  
const collection = collect(obj); 
  
collection.put(['Welcome to GeeksforGeeks']); 
  
console.log(collection.all());

输出:

{
  name:'Rahul',
  dob:'25-10-96',
  section:'A',
  score:98,
  'Welcome to GeeksforGeeks':undefined
}

相关用法


注:本文由纯净天空筛选整理自AshokJaiswal大神的英文原创作品 Collect.js put() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。