Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Return an array of names

Krygore

Bronze Coder
So I have an object array of classic artists, I need to create a function that creates a new array with just the artist names. I have tried using .map() and for..in loops with no success. Here is where I left off before giving up :sleep:

JavaScript:
const artists = [
  {
    "id": 0,
    "name": "Amedeo Modigliani",
    "years": "1884 - 1920",
    "genre": "Expressionism",
    "nationality": "Italian",
    "bio": "Amedeo Clemente Modigliani (Italian pronunciation: [ameˈdɛːo modiʎˈʎaːni]; 12 July 1884 – 24 January 1920) was an Italian Jewish painter and sculptor who worked mainly in France. He is known for portraits and nudes in a modern style characterized by elongation of faces, necks, and figures that were not received well during his lifetime but later found acceptance. Modigliani spent his youth in Italy, where he studied the art of antiquity and the Renaissance. In 1906 he moved to Paris, where he came into contact with such artists as Pablo Picasso and Constantin Brâncuși. By 1912 Modigliani was exhibiting highly stylized sculptures with Cubists of the Section d'Or group at the Salon d'Automne.",
    "wikipedia": "http://en.wikipedia.org/wiki/Amedeo_Modigliani",
    "paintings": 193
  },
  {
    "id": 1,
    "name": "Vasiliy Kandinskiy",
    "years": "1866 - 1944",
    "genre": "Expressionism,Abstractionism",
    "nationality": "Russian",
    "bio": "Wassily Wassilyevich Kandinsky (Russian: Васи́лий Васи́льевич Канди́нский, tr. Vasíliy Vasílʹevich Kandínskiy) (16 December [O.S. 4 December] 1866 – 13 December 1944) was a Russian painter and art theorist.",
    "wikipedia": "http://en.wikipedia.org/wiki/Wassily_Kandinsky",
    "paintings": 88
  },
  {
    "id": 2,
    "name": "Diego Rivera",
    "years": "1886 - 1957",
    "genre": "Social Realism,Muralism",
    "nationality": "Mexican",
    "bio": "Diego María de la Concepción Juan Nepomuceno Estanislao de la Rivera y Barrientos Acosta y Rodríguez, known as Diego Rivera (Spanish pronunciation: [ˈdjeɣo riˈβeɾa]; December 8, 1886 – November 24, 1957) was a prominent Mexican painter. His large frescoes helped establish the Mexican mural movement in Mexican art. Between 1922 and 1953, Rivera painted murals in, among other places, Mexico City, Chapingo, Cuernavaca, San Francisco, Detroit, and New York City. In 1931, a retrospective exhibition of his works was held at the Museum of Modern Art in New York. Rivera had a volatile marriage with fellow Mexican artist Frida Kahlo.",
    "wikipedia": "http://en.wikipedia.org/wiki/Diego_Rivera",
    "paintings": 70
  },
  {
    "id": 3,
    "name": "Claude Monet",
    "years": "1840 - 1926",
    "genre": "Impressionism",
    "nationality": "French",
    "bio": "Oscar-Claude Monet (; French: [klod mɔnɛ]; 14 November 1840 – 5 December 1926) was a French painter, a founder of French Impressionist painting and the most consistent and prolific practitioner of the movement's philosophy of expressing one's perceptions before nature, especially as applied to plein air landscape painting. The term \"Impressionism\" is derived from the title of his painting Impression, soleil levant (Impression, Sunrise), which was exhibited in 1874 in the first of the independent exhibitions mounted by Monet and his associates as an alternative to the Salon de Paris.Monet's ambition of documenting the French countryside led him to adopt a method of painting the same scene many times in order to capture the changing of light and the passing of the seasons. From 1883, Monet lived in Giverny, where he purchased a house and property and began a vast landscaping project which included lily ponds that would become the subjects of his best-known works. In 1899, he began painting the water lilies, first in vertical views with a Japanese bridge as a central feature and later in the series of large-scale paintings that was to occupy him continuously for the next 20 years of his life.",
    "wikipedia": "http://en.wikipedia.org/wiki/Claude_Monet",
    "paintings": 73
  }

    
    
function listOfNames(array) {
  let newArray = array;

  for (i = 0; i < array.length; i ++){
    return array.name;
  }
}
  console.log(listOfNames(artists));
 
Solution
I ended up figuring it out with a map method.


JavaScript:
function listOfNames(array) {
  let newArray = array.map(({name}) => name) ;
  return newArray;
}
  console.log(listOfNames(artists));
I spotted a number of errors here. First, you omit the closing bracket for artists[]. Then, your listOfNames() function is flawed in three different ways. This one works :

JavaScript:
function listOfNames(array)
{
  var newArray = [];
  for (i = 0; i < array.length; i ++)
  {
    newArray.push(array[i].name);
  }
  return newArray;
}

Spot the differences!

Off-topic: Good choice of painters. Though IMO Modigliani is very overrated, Monet a little less so. Kandinsky is great, Rivera is awesome. Don't forget to include Paul Klee, Georges Braque, and Pablo Picasso just to name some.
 
So, that returns just the first name in the array... Amedeo Modigliani

Off-topic: Good choice of painters. Though IMO Modigliani is very overrated, Monet a little less so. Kandinsky is great, Rivera is awesome. Don't forget to include Paul Klee, Georges Braque, and Pablo Picasso just to name some.
It's a pre-generated list TBH. There is a total of 20 artists, Paul Klee and Georges Braque are not on it, but Picasso is
 
I spotted a number of errors here. First, you omit the closing bracket for artists[]. Then, your listOfNames() function is flawed in three different ways. This one works :

JavaScript:
function listOfNames(array)
{
  var newArray = [];
  for (i = 0; i < array.length; i ++)
  {
    newArray.push(array[i].name);
  }
  return newArray;
}

Spot the differences!

Off-topic: Good choice of painters. Though IMO Modigliani is very overrated, Monet a little less so. Kandinsky is great, Rivera is awesome. Don't forget to include Paul Klee, Georges Braque, and Pablo Picasso just to name some.
you mentioned that the function is flawed in three different ways, but failed to mention which... might be helpful to follow through and explain where the flaws are. Even if the user is able to spot them, it may be helpful to explain why they are flaws.
 
Last edited:
I ended up figuring it out with a map method.


JavaScript:
function listOfNames(array) {
  let newArray = array.map(({name}) => name) ;
  return newArray;
}
  console.log(listOfNames(artists));
 
Solution
you mentioned that the function is flawed in three different ways, but failed to mention which... might be helpful to follow through and explain where the flaws are. Even if the user is able to spot them, it may be helpful to explain why they are flaws.
Generally I agree. In this case I assumed posting the corrected code of this tiny function and saying "spot the differences" would make the flaws obvious :geek:
For the record then, the flaws were
  • defining newArray but not using it
  • using array.name (which is undefined) instead of array[i].name
  • doing an unconditional return from inside the loop so the loop hits only the first element
A little spot of debugging would surely have revealed all this too ;)
The map solution is of course to be preferred, in hindsight.
 

Buy us a coffee!

Back
Top Bottom