0

I'm having an array look like this

 [
  { 
    object1:{ childObj1:[grandChild1,grandChild2], childObj1, childObj1}
  },
  { 
    object2:{ childObj1:[grandChild1,grandChild2], childObj1, childObj1}
  },
  { 
    object3:{ childObj1:[grandChild1,grandChild2], childObj1, childObj1}
  },

 ]

Now i want to get rid of every nested item that have more than one item and keep only the first, like :

 [
  { 
    object1:{ childObj1:[grandChild1]}
  },
  { 
    object2:{ childObj2:[grandChild1]}
  },
  { 
    object3:{ childObj3:[grandChild1]}
  },

 ]

Which is the most effective way to do this?

CODEforDREAM
  • 863
  • 1
  • 8
  • 24
  • `object1:[ childObj1:[grandChild1]]`. `childObj1` in `[]` is not an object. I'm not sure how you define `childObj1` – Nick Vu May 05 '22 at 03:37
  • Ah my false, let me fix the syntax – CODEforDREAM May 05 '22 at 03:45
  • Does this answer your question? [How can I access and process nested objects, arrays or JSON?](https://stackoverflow.com/questions/11922383/how-can-i-access-and-process-nested-objects-arrays-or-json) – Edwin May 05 '22 at 05:11

2 Answers2

1

const arr = [{
    object1: {
      childObj1: ['grandChild1', 'grandChild2'],
      childObj3: 't',
      childObj2: 'r'
    }
  },
  {
    object2: {
      childObj1: ['grandChild1', 'grandChild2'],
      childObj3: 't',
      childObj2: 'r'
    }
  },
  {
    object3: {
      childObj1: ['grandChild1', 'grandChild2'],
      childObj3: 't',
      childObj2: 'r'
    }
  },

];

console.log(arr.map(el => {
  const child = el[Object.keys(el)[0]];
  const firstProperty = {
    [Object.keys(child)[0]]: child[Object.keys(child)[0]]
  };
  return {
    [Object.keys(el)[0]]: firstProperty
  }
}));

Hope this is what you wanted.

A G
  • 21,087
  • 11
  • 87
  • 112
1

Try this:

const arr = [{
    object1: {
      childObj1: ['grandChild1', 'grandChild2'],
      childObj2: {},
      childObj3: {}
    }
  },
  {
    object2: {
      childObj1: ['grandChild1', 'grandChild2'],
      childObj2: {},
      childObj3: {}
    }
  },
  {
    object3: {
      childObj1: ['grandChild1', 'grandChild2'],
      childObj2: {},
      childObj3: {}
    }
  },
]

function f(obj, startLevel, currentLevel = 0) {
  let value
  if (Array.isArray(obj)) {
    value = []
  } else if (typeof obj === 'object') {
    value = {}
  } else {
    value = obj
  }

  if (typeof obj === 'object') {
    for (let i in obj) {
      if (currentLevel >= startLevel) {
        if (Array.isArray(obj[i])) {
          value[i] = obj[i].length ? [f(obj[i][0], startLevel, currentLevel + 1)] : [];
        } else {
          value[i] = f(obj[i], startLevel, currentLevel + 1)
        }
        break;
      } else {
        value[i] = f(obj[i], startLevel, currentLevel + 1)
      }
    }
  }

  return value;
}

console.log(f(arr, 1));
Leon
  • 131
  • 4
  • Thank for your answer, it work well but still have small problem, if the item contain multi object, it still have more than one value, i just want to keep one value of all object and array – CODEforDREAM May 05 '22 at 07:32
  • 1
    I think you mean the left item should also be handled, and update the code, please try again, thanks! – Leon May 05 '22 at 08:31
  • 1
    Update to only keep one value of all object and array – Leon May 05 '22 at 10:59