skip to Main Content

I have an array of objects, which has an array of objects inside (it also has another one inside).

const array = [
  {
    id: 1,
    prod: [
      {
        id: 1,
        op: [
          {
            id: 1
          },
          {
            id: 2
          }
        ]
      },
      {
        id: 2,
        op: [
          {
            id: 1
          }
        ]
      }
    ]
  },
  {
    id: 2,
    prod: [
      {
        id: 1,
        op: [
          {
            id: 1
          },
          {
            id: 2
          }
        ]
      },
      {
        id: 2,
        op: [
          {
            id: 1
          }
        ]
      }
    ]
  }
];

The question is how to enter a new key value in each one so that it looks like this:

[
  {
    id: 1,
    isChecked: false,
    prod: [
      {
        id: 1,
        isChecked: false,
        op: [
          {
            id: 1,
            isChecked: false
          },
          {
            id: 2,
            isChecked: false
          }
        ]
      },
      {
        id: 2,
        isChecked: false,
        op: [
          {
            id: 1,
            isChecked: false
          }
        ]
      }
    ]
  },
  {
    id: 2,
    isChecked: false,
    prod: [
      {
        id: 1,
        isChecked: false,
        op: [
          {
            id: 1,
            isChecked: false
          },
          {
            id: 2,
            isChecked: false
          }
        ]
      },
      {
        id: 2,
        isChecked: false,
        op: [
          {
            id: 1,
            isChecked: false
          }
        ]
      }
    ]
  }
];

I know that iterating can be achieved

array.map(object => {
  return {
    ...object,
    isChecked: false
  }
})

but is there a better way to do it?
some way that all objects have that key/value without iterating multiple times

2

Answers


  1. You can do it recursively regardless of the number and names of the array properties. The only problem you should insert the isChecked after id so we should do that explicitly.

    Note that we don’t use Object.XXX methods and spread syntax here, because they are slow.

    const addCheck = arr => arr.map(item => {
      const out = {};
      for(const key in item) { 
        const val = item[key];
        out[key] = Array.isArray(val) ? addCheck(val) : val; 
        if(key === 'id') out.isChecked = false;
      }
      return out;
    });
    
    console.log(addCheck(array));
    <script>
    const array = [
      {
        id: 1,
        prod: [
          {
            id: 1,
            op: [
              {
                id: 1
              },
              {
                id: 2
              }
            ]
          },
          {
            id: 2,
            op: [
              {
                id: 1
              }
            ]
          }
        ]
      },
      {
        id: 2,
        prod: [
          {
            id: 1,
            op: [
              {
                id: 1
              },
              {
                id: 2
              }
            ]
          },
          {
            id: 2,
            op: [
              {
                id: 1
              }
            ]
          }
        ]
      }
    ];
    
    </script>
    Login or Signup to reply.
  2. You can do it recursively with a one-liner like this:

    const array = [{"id":1,"prod":[{"id":1,"op":[{"id":1},{"id":2}]},{"id":2,"op":[{"id":1}]}]},{"id":2,"prod":[{"id":1,"op":[{"id":1},{"id":2}]},{"id":2,"op":[{"id":1}]}]}]
    
    const f = v => v instanceof Object && (
      !Array.isArray(v) && (v.isChecked = false),
      Object.values(v).forEach(f)
    )
    
    f(array)
    console.log(array)

    It adds isChecked = false only for regular (non-array) objects, and recurses for all items in the object (which will include both object property values and array elements).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search