Home > Blockchain >  Remove surrounding array from array of object
Remove surrounding array from array of object

Time:02-12

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":"[email protected]"}] Also, would the solution work in the case of multiple objects in the array? Thanks!

CodePudding user response:

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":"[email protected]"}, {"id":"12","email":"[email protected]"}];
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>

  • Related