skip to Main Content

The code i tried but no change in array

const data = [
  {
    name: 'Fruit',
    children: [{ name: 'Apple' }, { name: 'Banana' }, { name: 'Fruit loops' }],
  },
  {
    name: 'Vegetables',
    children: [
      {
        name: 'Green',
        children: [{ name: 'Broccoli' }, { name: 'Brussels sprouts' }],
      },
     {
        name: 'Orange',
        children: [{ name: 'Pumpkins' }, { name: 'Carrots' }],
      },
    ],
  },
];

output needed

 const data = [
   {
     name: 'Fruit',
     children: [{ name: 'Apple' }, { name: 'Banana' }, { name: 'Fruit loops' }],
   },
   {
     name: 'Vegetables',
     children: [
       {
         name: 'Green',
         children: [{ name: 'Broccoli' }, { name: 'Brussels sprouts' }],
       },
       {
         name: 'Orange',
         children: [{ name: 'Pumpkins' },{name:'newname'} { name: 'Carrots' }],
       },
     ],
   },
 ];

function addObjectBefore(arr, targetName, newObj) {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i].children) {
      addObjectBefore(arr[i].children, targetName, newObj); // Recursive call for nested children array
    }
    if (arr[i].name === targetName) {
      const targetIndex = arr[i]?.children?.findIndex(obj => obj.name === targetName);
      arr[i]?.children?.splice(targetIndex, 0, newObj); // Insert new object before the target
      break; // Stop the iteration once the target is found and object is inserted
    }
  }
}

// Given data
const data = [
  {
    name: 'Fruit',
    children: [{ name: 'Apple' }, { name: 'Banana' }, { name: 'Fruit loops' }],
  },
  {
    name: 'Vegetables',
    children: [
      {
        name: 'Green',
        children: [{ name: 'Broccoli' }, { name: 'Brussels sprouts' }],
      },
      {
        name: 'Orange',
        children: [{ name: 'Pumpkins' }, { name: 'Carrots' }],
      },
    ],
  },
];

// Call the function to add the new object
addObjectBefore(data, 'Pumpkins', { name: 'new name' });

// Output the modified data
console.log(data);

2

Answers


  1. function addObjectBefore(arr, targetName, newObj) {
      for (let i = 0; i < arr.length; i++) {
        if (arr[i].children) {
          const targetIndex = arr[i].children.findIndex(obj => obj.name === targetName);
          if (targetIndex !== -1) {
            arr[i].children.splice(targetIndex, 0, newObj); // Insert new object before the target
            return; // Stop the iteration once the target is found and object is inserted
          } else {
            addObjectBefore(arr[i].children, targetName, newObj); // Recursive call for nested children array
          }
        }
      }
    }
    
    const data = [
      {
        name: 'Fruit',
        children: [{ name: 'Apple' }, { name: 'Banana' }, { name: 'Fruit loops' }],
      },
      {
        name: 'Vegetables',
        children: [
          {
            name: 'Green',
            children: [{ name: 'Broccoli' }, { name: 'Brussels sprouts' }],
          },
          {
            name: 'Orange',
            children: [{ name: 'Pumpkins' }, { name: 'Carrots' }],
          },
        ],
      },
    ];
    
    addObjectBefore(data, 'Pumpkins', { name: 'new name' });
    
    // Output the modified data
    console.log(data);
    Login or Signup to reply.
  2. After calling Array::findIndex you should actually check whether the index is found, it should be >= 0:

    const data = [
      {
        name: 'Fruit',
        children: [{ name: 'Apple' }, { name: 'Banana' }, { name: 'Fruit loops' }],
      },
      {
        name: 'Vegetables',
        children: [
          {
            name: 'Green',
            children: [{ name: 'Broccoli' }, { name: 'Brussels sprouts' }],
          },
         {
            name: 'Orange',
            children: [{ name: 'Pumpkins' }, { name: 'Carrots' }],
          },
        ],
      },
    ];
    
    insert(data, 'Pumpkins', {name: 'newname'});
    
    console.log(data);
    
    
    function insert(data, name, what){
      for(const child of data){
        if(child.children){
          const foundIdx = child.children.findIndex(item => item.name === name);
          if(foundIdx >= 0){
            child.children.splice(foundIdx + 1, 0, what);
            return true;
          }
          if(insert(child.children, name, what)){
            return true;
          }
        }
      }
       
    }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search