does anyone know how I can remove a surrounding array from an array of objects? In my case, I only have one object in this array. [{"id":"6","email":"test@test.com"}]
Also, would the solution work in the case of multiple objects in the array? Thanks!
You have an array of objects. That's great, because it's the easiest way to store and manipulate lists of records.
You can use array methods like .map that allow you to treat each record separately, telling it what to do individually with each element. That's also great, because it basically "removes the element from the array" while still processing the whole array, which is what I think you're after.
Simple example to create a dropdown:
const data = [{"id":"6","email":"test@test.com"}, {"id":"12","email":"test2@test.com"}];
const drawEmailDropdown = () => {
let options = data.map(d => `<option value='${d.id}'>${d.email}</option>`);
return `<select>` + options.join("") + `</select>`;
};
document.querySelector("#container").innerHTML = drawEmailDropdown();
<div id="container"></div>