0

Hi Guys,

I have a array with unknown amount of data in it and each data object may have many subitems,

var array = [
  {
    item: 1,
    subItem: [
      {
        item: 2,
        subItem: [
          {
            item: 2,
            subItem: [
              { item: 2 },
              { item: 3 },
              { item: 4 }
            ]
          },
          { item: 3 },
          { item: 4 }
        ]
      },
      { item: 3 },
      { item: 4 }
    ]
  },
  {
    item: 1,
    subItem: [
      {
        item: 2,
        subItem: [
          {
            item: 2,
            subItem: [
              { item: 2 },
              { item: 3 },
              { item: 4 }
            ]
          },
          { item: 3 },
          { item: 4 }
        ]
      },
      { item: 3 },
      { item: 4 }
    ]
  }
];

I have target in string format

let target = 'array[0].subItem[2]';

I need to update this target in array

I can't loop because target will be random and it can 100th child.

Please help me update this target with this update

let update = {item: 'newitem'};

I can update target by

target = update;

but now how will i update value in main array? jsfiddle

4
  • 3
    does stackoverflow.com/q/40131843/1447675 work for you? Commented Jan 2, 2020 at 20:49
  • I can't loop because i dont know the number of child components, but its very interesting Commented Jan 2, 2020 at 20:51
  • what is the poblem by taking a string and address all properties by reduceing the object? Commented Jan 2, 2020 at 20:54
  • data target is string, i need to update main array, maybe your answer is the solution, i am checking it Commented Jan 2, 2020 at 20:55

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.