I have an array of objects. Is it possible to print key values using array.map method?
<div> +
array.map( x => {
return `<span class="key">"value"</span><br>
}) +
</div>
I have an array of objects. Is it possible to print key values using array.map method?
<div> +
array.map( x => {
return `<span class="key">"value"</span><br>
}) +
</div>
Try something like this:
const array = [{
key: '1',
value: 'value-1'
}, {
key: '2',
value: 'value-2'
}];
const text = array.map(x => `<span class="${x.key}">${x.value}</span><br>`).join("");
const element = document.querySelector('#log');
element.innerHTML = text;
<div id="log"></div>
Object.entries should be the right choice