Hello!

Tell me, please, how in javascript (without jQuery) in a forEach loop, fill the object with elements and put this received object into an array:

newArr = []; newObj = {}; Array.prototype.slice.call(document.querySelectorAll('#mydiv a')).forEach(function(e) { data = e.getAttribute('data'); //строка, например "Фрукты" value = e.innerHTML; //строка - "Апельсин" //тут надо вставить в объект полученные данные data и value }); 

As a result, you need to get an object inside the array of this structure:

 newArr= [ {Фрукты: "Апельсин"}, {Овощи: "Томат"}; ] 

Thank!

    1 answer 1

    The map method is suitable for this, and the property names to be calculated are added in ES2015.

     newArr = Array.prototype.map.call(document.querySelectorAll('#mydiv a'), function(el){ return { [el.getAttribute('data')] : el.innerHTML }; });