2

I have my object that is stored in variable val.Main.widgets. I also have variable that functions as a data tree. What I need is to generate as many children elements as there are keys in my object that is stored in val.Main.widgets. If I console log this: console.log(Object.keys(val.Main.widgets).length;, it returns 8 so in this case I need to generate 8 children elements.

I am guessing I need some sort of cycle, but I really dont know where to start, hence why I am asking here.

Here is my object:

enter image description here

And here is my tree variable:

const tileTree = [
  {
    name: val.Main.name,
    children: [
      {
        name: val.Main.widgets['E1EV7'].name,
      },
      {
        name: val.Main.widgets['E70ZT'].name,
      },
    ],
  },
];

Thank you for any advice.

AdamSulc
  • 330
  • 2
  • 4
  • 19

4 Answers4

2

You do not need lodash for this. You want to use Array.map on the result of Object.keys.

const content = val.Main.widgets;
const keys = Object.keys(content);
const children = keys.map(key => content[key]);

Then in your tileTree you simply set children to children.

const tileTree = [
  {
    name: val.Main.name,
    children,
  },
];

This will give you all the properties of the val.Main.widgets object. If you only want specific ones, you can destructure them in your map function.

...
// Suppose we only want 'name'.
const children = keys.map(key => {
  const { name } = content[key];
  return { name };
});
...
Rodrigo Ehlers
  • 1,830
  • 11
  • 25
0

You can use

Object.keys(val.Main.widgets).map(widgetKey => {
   const widget = val.Main.widgets[widgetKey]

   return (
       <div key={widgetKey}>
          <h1>This is widget : {widget.name}</h1>
       </div>
   )
}
Geoffrey H
  • 1,280
  • 2
  • 10
  • 33
0

Object.keys is a function that returns an array. The array has a foreach method

var lunch = {
    sandwich: 'ham',
    snack: 'chips',
    drink: 'soda',
    desert: 'cookie',
    guests: 3,
    alcohol: false,
};

Object.keys(lunch).forEach(function (item) {
    console.log(item); // key
    console.log(lunch[item]); // value
});

Other way is by using lodash module from npm or yarn. example where _ is the lodash module:

_.times(8, i => {
    schedule.push({
        date: moment()
            .add(i, "days")
            .format("YYYY-MM-DD"),
        times: [8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
    });
});

You can replace the 8 with your .length code.

  • `Object.keys` is a function that returns an array. The _array_ has a `.forEach` method... – Mulan Oct 03 '19 at 12:52
0
const tileTree = [
  {
    name: 'test',
    children: [
      {
        name: 'test1'
      },
      {
        name: 'test2'
      },
      {
        name: 'test3'
      },
            {
        name: 'test4'
      },
            {
        name: 'test5'
      },
            {
        name: 'test6'
      }
    ],
  },
];
    const createLi = (arr) => {
        for (let i = 0; i < arr[0].children.length; i += 1) {
        let li = document.createElement("li");
        let ol = document.getElementById('list');
        li.innerHTML = arr[0].children[i].name;
        ol.appendChild(li);
      }
    }

    createLi(tileTree);

https://jsfiddle.net/os20wdLh/

Altair312
  • 328
  • 3
  • 13