1

I have following data:

    const section={
     fileds: [
     {
      child: [
        {
          fileds: [
            {
              id: "kxf5",
              label: null
            }
          ]
        },
        {
          fileds: [
            {
              id: "ed5t",
              label: "section"
            }
          ]
        },
        
      ]
    },
    {
      child: [
        {
          fileds: [
            {
              id: "ccfr",
              label: null
            }
          ]
        },
        {
          fileds: [
            {
              id: "kdpt8",
              label: "section"
            }
          ]
        },
        
      ]
    },
    
     ]
    }

I need to return all id in array and I should use recursion.

I have try following code.

const section={fileds: [{child: [{fileds: [{id: "kxf5",label: null}]},{fileds: [{id: "ed5t",label: "section"}]},]},{child: [{fileds: [{id: "ccfr",label: null}]},{fileds: [{id: "kdpt8",label: "section"}]},]},]}
function printId(value, child ) {
  return [value, ...(child ? printList(child) : [])]
}

console.log(printId(section.fields));

But it not helped me.

Is there a way to solve this problem with recursion? Please help to fix this.

The final result should be like ["kxf5","ed5t","ccfr", "kdpt8"] this.

1
  • 2
    You have a typo in your code: fileds. Commented Apr 26, 2022 at 13:38

3 Answers 3

1

An other way, using reduce() to recursively fill an array

const section= {fileds: [{child: [{fileds: [{id: "kxf5", label: null } ] }, {fileds: [{id: "ed5t", label: "section"} ] }, ] }, {child: [{fileds: [{id: "ccfr", label: null } ] }, {fileds: [{id: "kdpt8", label: "section"} ] }, ] }, ] }

function printId(value) {
    return value.reduce((prev, cur) => {
        if (cur.id) {
            return [ ...prev, cur.id ];
        } else {
            let key = ('child' in cur) ? 'child' : 'fileds';
            return [ ...prev, ...printId(cur[key]) ]
        }
    }, []);
}

console.log(printId(section.fileds));

Sign up to request clarification or add additional context in comments.

Comments

1

A better solution could be this one:

// recursive method to extract "id"s
function getId(o) {
  // if current parameter "o" has "id" prop, return it
  if (o.id) return o.id;
  // else, iterate over "o" and recurse for each "value" of "o"
  return Object.keys(o).flatMap((key) => getId(o[key]));
}
const section = {
  fileds: [{
      child: [{
          fileds: [{
            id: "kxf5",
            label: null
          }]
        },
        {
          fileds: [{
            id: "ed5t",
            label: "section"
          }]
        },

      ]
    },
    {
      child: [{
          fileds: [{
            id: "ccfr",
            label: null
          }]
        },
        {
          fileds: [{
            id: "kdpt8",
            label: "section"
          }]
        },

      ]
    },

  ]
};

console.log(getId(section));

Comments

0

Below is one possible way to achieve the target.

Code Snippet

// recursively get only "id"s
const getIds = arr => (
  // implicit return of below result
  // iterate using ".flatMap()" to avoid nested result
  arr.flatMap(obj => {
    // extract values that are arrays
    const arrArrs = Object.values(obj).filter(v => Array.isArray(v));
    // extract values for key "id"
    const arrVals = Object.entries(obj).filter(
      ([k, v]) => typeof v === 'string' && k === "id"
    ).map(([k, v]) => v);
    // return "id" values, and then recursively call getIds for each "ar" (array)
    return [...arrVals, ...arrArrs.flatMap(ar => getIds(ar))]
  })
);

const section = {
  fileds: [{
      child: [{
          fileds: [{
            id: "kxf5",
            label: null
          }]
        },
        {
          fileds: [{
            id: "ed5t",
            label: "section"
          }]
        },

      ]
    },
    {
      child: [{
          fileds: [{
            id: "ccfr",
            label: null
          }]
        },
        {
          fileds: [{
            id: "kdpt8",
            label: "section"
          }]
        },

      ]
    },

  ]
};

console.log(getIds(section.fileds));

Explanation

Inline comments added in the snippet above.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.