There are children in the array, how to get the value corresponding to a certain key of all objects in the array, form a new value and store it in the array

1. In actual development, we will encounter an array containing chidren in the array. If we want to obtain the corresponding value of a specified key in the array to form a new key and value, how can we change the simple and convenient operation at this time? Woolen cloth?

1: How to get all the id values ​​inside, including those in children

[
  {
    
    
       id: 1,
       age: 23,
       name: '测试1',
       children: [
         {
    
    
              id: 2,
              age: 24,
              name: '测试11',
              children: [
           {
    
    
               id: 3,
               age: 244,
               name: '测试111'
            },
        ]
         }
       ]
   },
   {
    
    
       id: 4,
       age: 26,
       name: '测试2',
       children: [],
   }
]
Method 1... The operator is used to solve:
let a = [
  {
    
    
       id: 1,
       age: 23,
       name: '测试1',
       children: [
         {
    
    
              id: 2,
              age: 24,
              name: '测试11',
              children: [
           {
    
    
               id: 3,
               age: 244,
               name: '测试111'
            },
        ]
         }
       ]
   },
   {
    
    
       id: 4,
       age: 26,
       name: '测试2',
       children: [],
   }
]

// (1)
function func(arr){
    
    
  return arr.reduce((a, b) => {
    
    
    let res = [...a,b.id];
    if (b.children) res = [...res, ...func(b.children)];
    return res;
  }, []);
}

// (2)
function func(arr) {
    
    
   return arr.reduce((a, b) => {
    
    
     return b.children
       ? [...a, b.id, ...func(b.children)]
       : [...a, b.id];
   }, []);
 }
console.log(func(a))  
// 结果: [1,2,3,4]
Method 2: Use for loop to traverse, and then recurse
// (1)
let arr= [
   {
    
     id: 1, children: [{
    
     id: 2 },{
    
     id: 3 }] },
   {
    
     id: 4, children: [] }
 ];
// 获取对应id
const getIds = function(arr) {
    
    
const res = arr.reduce((prev, next) => {
    
    
	 if (next.id) prev.push(next.id);
	 if (next.children && next.children.length) return prev.concat(getIds(next.children));
	    return prev;
	    }, []);
	    return res;
  };
 const res = getIds(arr);
 console.log(res);
 
 // 获取对应需要的key
 function getKeys(list, key) {
    
    
    return list.reduce((res, v) => {
    
    
        if(v.children && v.children.length) res = res.concat(getKeys(v.children,key))
        res.push(v[key]);
        return res;
    }, [])
}
console.log(getKeys(arr, 'id'))

// (2)
const getIdsArry = (arr) => {
    
    
  const ans = [];

  const dfs = (root, ans) => {
    
    
    let neighbor;
    if (Array.isArray(root)) {
    
    
      neighbor = root;
    } else {
    
    
      ans.push(root.id);
      neighbor = root.children;
    }

    if (!neighbor) return;

    for (let i = 0; i < neighbor.length; ++i) {
    
    
      dfs(neighbor[i], ans);
    }
  };

  dfs(arr, ans);

  return ans;
};
console.log(getIds (arr))

2: Same as the borrowing structure above, get the corresponding id [2,3] in children

let a = [
   {
    
     id: 1, children: [{
    
     id: 2 },{
    
     id: 3 }] },
   {
    
     id: 4, children: [] }
 ];
 function func(arr) {
    
    
   return arr.reduce((a, b) => {
    
    
     return b.children ? [...a, ...func(b.children)] : [...a, b.id];
   }, []);
 }
 console.log(func(a));
 // 结果  [2,3]

3. Form a new value and store it in the array

insert image description here

var aa = [
  {
    
    
        id: 1,
        age: 23,
        name: '测试1',
        children: [
          {
    
    
               id: 2,
               age: 24,
               name: '测试11',
               children: [
	           {
    
    
                id: 3,
                age: 244,
                name: '测试111'
	            },
	        ]
          }
        ]
    },
    {
    
    
        id: 4,
        age: 26,
        name: '测试2',
        children: [],
    }
    ]
for (var itemproject = 0; itemproject < aa.length; itemproject++) {
    
    
     if (aa[itemproject].children && aa[itemproject].children.length > 0) {
    
    
        aa[itemproject].selectedFields = getidarry(aa[itemproject].children)
      } else {
    
    
        aa[itemproject].selectedFields = []
      }
}
function getidarry (arr) {
    
    
  	  return arr.reduce((a, b) => {
    
    
	   let res = [...a,b.id];
	   if (b.children) res = [...res, ...getidarry(b.children)];
	   return res;
	 }, []);
}
console.log('新数组', aa)

Guess you like

Origin blog.csdn.net/qq_44552416/article/details/129281633