当前位置:网站首页>Array object in JS

Array object in JS

2022-06-27 10:16:00 Xiaaoke

Array object de duplication

Determine the de duplication according to one word end of the array object

  • Parameters
    • arr: The array to be duplicated ( Array objects )
    • id: According to the de duplication terminal name (string)
  • Return value : Merged array objects ( Array objects )
function arrObjUniq(arr, id){
    
	let obj = {
    };
	arr = arr.reduce((cur, next)=>{
    
		obj[next[id]] ? '' : obj[next[id]] = true && cur.push(next);
		return cur;
	})
	return arr;
}

Array objects are merged in the same way

Judge the merge according to one word end of the array object

  • Parameters
    • object: Target audience ( Array objects )
    • sources: Source object ( Array objects )
    • id: Merge word ends according to (string)
  • Return value : Merged array objects ( Array objects )
function objMerge(object,sources,id){
    
	const c = [];
	object.forEach(item=>
		sources(val => item[id] === val[id] && c.push({
    ...item, ...val}))
	);
	return c;
}

Array object grouping

const groups = dataObj.reduce((ini, product) => {
    
   const [targetGroup] = ini.filter((grp) => {
    
     const [first] = grp;
     return first.grade === product.grade;
   });
   if (targetGroup) targetGroup.push(product)
   else ini.push([product]);
   return ini;
 }, []);
原网站

版权声明
本文为[Xiaaoke]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/178/202206270914372198.html

随机推荐