0

I am trying to change objects in nested map functions. I have one array of objects with nested data.

If I try to do on this way structure of data is changing. I'm getting array of arrays. Only what I need here is to add for each object one property "level". Is it possible to do this with nested map functions?

const res = data.map(itemA => ({ ...itemA,
    level: 'a'
  })
  .subA.map(itemB => ({ ...itemB,
    level: 'b'
  })))

console.log(res)
<script>
  let data = [{
      name: 'testA1',
      subA: [{
          name: 'testB1',
          subB: [{
            name: 'testC1',
            subC: []
          }]
        },
        {
          name: 'testB2',
          subB: [{
            name: 'testC1',
            subC: []
          }]
        }
      ]
    },
    {
      name: 'testA2',
      subA: [{
        name: 'testB1',
        subB: [{
          name: 'testC1',
          subC: [{
            name: 'testD1',
            subD: []
          }]
        }]
      }]
    }
  ]
</script>

2
  • Missed a comma. I made you a snippet Commented Dec 13, 2021 at 19:51
  • Could you provide an example on what the result for your example shall look like? Commented Dec 13, 2021 at 20:23

2 Answers 2

1

You can use a recursive function?

let data = [{ name: 'testA1', subA: [{ name: 'testB1', subB: [{ name: 'testC1', subC: [] }] }, { name: 'testB2', subB: [{ name: 'testC1', subC: [] }] } ] }, { name: 'testA2', subA: [{ name: 'testB1', subB: [{ name: 'testC1', subC: [{ name: 'testD1', subD: [] }] }] }] } ]

function addprop(arr, level){
    arr.map(x=>{
        const match = Object.keys(x).find(element => {
            if (element.includes('sub')) {
              return true;
            }
          });
          addprop(x[match], level+1)
    })
    return arr.map(x=>(x.level=level,x))
}

console.log(addprop(data, 1))

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

Comments

0

Actually I did it on this way:

const res = data.map(itemA => (
  {...itemA, level: 'a', subA: itemA.subA.map(itemB => (
    {...itemB, level: 'b', subB: itemB.subB.map(itemC => (
      {...itemC, level: 'c'}
    ))}
  ))}
))
console.log(res)
<script>
  let data = [{
      name: 'testA1',
      subA: [{
          name: 'testB1',
          subB: [{
            name: 'testC1',
            subC: []
          }]
        },
        {
          name: 'testB2',
          subB: [{
            name: 'testC1',
            subC: []
          }]
        }
      ]
    },
    {
      name: 'testA2',
      subA: [{
        name: 'testB1',
        subB: [{
          name: 'testC1',
          subC: [{
            name: 'testD1',
            subD: []
          }]
        }]
      }]
    }
  ]
</script>

2 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
your answer only works to level c

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.