How to display same property from different objects in Javascript

tomgru23

I am doing a project which uses The Movie Db API and I am bit confused with displaying following object:

(3) [{…}, {…}, {…}]
0: {id: 12, name: "Adventure"}
1: {id: 28, name: "Action"}
2: {id: 878, name: "Science Fiction"}
length: 3
__proto__: Array(0)

Basically, I am trying to figure out how to display those 3 genres in one element. Doing forEach loop like this :

const genreArr = data.genres;
console.log(genreArr)
genreArr.forEach(function(item){
    genre.textContent = item.name;
})

displays only one name of genre rather than all of them. So, what do I have to do to display all of those in one paragraph?

Turnip

This line genre.textContent = item.name; will overwrite the textContent every iteration of forEach leaving only the last value.

You need append to textContent using +=.

const genreArr = [
  {id: 12, name: "Adventure"},
  {id: 28, name: "Action"},
  {id: 878, name: "Science Fiction"}
];

const genre = document.getElementById('genre');

genreArr.forEach(function(item){
    genre.textContent += item.name + ' | ';
})
<div id="genre">

</div>

An alternate, more efficient method using map() and join():

const genreArr = [
  {id: 12, name: "Adventure"},
  {id: 28, name: "Action"},
  {id: 878, name: "Science Fiction"}
];

const genre = document.getElementById('genre');

/* Create an array containing only the 'name' values */
var genreNames = genreArr.map(function(item) {
  return item['name'];
});

/* Assign the names to the div in one go, with a seperator */
genre.textContent = genreNames.join(' | ');
<div id="genre"></div>

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Inherited child objects share the same array property in JavaScript?

분류에서Dev

how is Array object different from other objects

분류에서Dev

how to display dates from two different tables?

분류에서Dev

how to make method calls of multiple objects of same class in different thread?

분류에서Dev

How to click through array of objects and display data in div using javascript

분류에서Dev

Update the array with attributes from different array with same key for both - javascript

분류에서Dev

How can I use the same ViewController from a storyboard with different sizes?

분류에서Dev

Comparing of the 2 lists of different objects but with a same fields

분류에서Dev

grep command to display same word with different spellings

분류에서Dev

Binding c# Property to a different Property on the SAME control

분류에서Dev

How to remove a property from a JavaScript object when using Strict Mode

분류에서Dev

Why different UIs display different size of the same file?

분류에서Dev

How to refer to each property of an object in an array of objects in MongoDB MapReduce JavaScript query?

분류에서Dev

How do javascript engines set properties on objects that have prototypes with a setter for that property?

분류에서Dev

How to get array of objects from current collection and data from another collection in same query

분류에서Dev

How to count data from a column in database which will have 3 different values and display it in Graph?

분류에서Dev

How can I display data in one table from different sql statement?

분류에서Dev

how to display values from 2 different arrays in single ng-repeat?

분류에서Dev

how to display popup methods for Excel objects in VBA?

분류에서Dev

How can I display an array of objects?

분류에서Dev

How to get clicked element from multiple element with the same name different index index of array

분류에서Dev

How can I link different MP3 files from an <ul> playlist to the same <audio> tag?

분류에서Dev

copy lane from different sheet if the same value

분류에서Dev

Fetch data from same table with different condition

분류에서Dev

Listen to same result from different Android Fragments

분류에서Dev

OpenCv subtract same image but from different sources

분류에서Dev

MonoGame - calling the same function from different classes result in different behavior

분류에서Dev

How to change results to display a different name SQL

분류에서Dev

Change style.display on multiple objects through one button JAVASCRIPT

Related 관련 기사

  1. 1

    Inherited child objects share the same array property in JavaScript?

  2. 2

    how is Array object different from other objects

  3. 3

    how to display dates from two different tables?

  4. 4

    how to make method calls of multiple objects of same class in different thread?

  5. 5

    How to click through array of objects and display data in div using javascript

  6. 6

    Update the array with attributes from different array with same key for both - javascript

  7. 7

    How can I use the same ViewController from a storyboard with different sizes?

  8. 8

    Comparing of the 2 lists of different objects but with a same fields

  9. 9

    grep command to display same word with different spellings

  10. 10

    Binding c# Property to a different Property on the SAME control

  11. 11

    How to remove a property from a JavaScript object when using Strict Mode

  12. 12

    Why different UIs display different size of the same file?

  13. 13

    How to refer to each property of an object in an array of objects in MongoDB MapReduce JavaScript query?

  14. 14

    How do javascript engines set properties on objects that have prototypes with a setter for that property?

  15. 15

    How to get array of objects from current collection and data from another collection in same query

  16. 16

    How to count data from a column in database which will have 3 different values and display it in Graph?

  17. 17

    How can I display data in one table from different sql statement?

  18. 18

    how to display values from 2 different arrays in single ng-repeat?

  19. 19

    how to display popup methods for Excel objects in VBA?

  20. 20

    How can I display an array of objects?

  21. 21

    How to get clicked element from multiple element with the same name different index index of array

  22. 22

    How can I link different MP3 files from an <ul> playlist to the same <audio> tag?

  23. 23

    copy lane from different sheet if the same value

  24. 24

    Fetch data from same table with different condition

  25. 25

    Listen to same result from different Android Fragments

  26. 26

    OpenCv subtract same image but from different sources

  27. 27

    MonoGame - calling the same function from different classes result in different behavior

  28. 28

    How to change results to display a different name SQL

  29. 29

    Change style.display on multiple objects through one button JAVASCRIPT

뜨겁다태그

보관