javascript - React - result of forEach() is undefined - Stack Overflow

I am attempting to render out prop values from a nested object. I'm running into two issues: 1. JS

I am attempting to render out prop values from a nested object. I'm running into two issues: 1. JSX line of code (containing variables) is not rendering in the browser 2. console logging the variables, containing the .forEach() methods, return 'undefined'

Question: How can I access the nested prop values, pass them into the JSX code, and render in the browser?

Current state of my code:

//data object
project1: {
  otherData: 'value',
  technology: [ 
    {
      name: 'HTML',
      logo: 'logoTest.png',
      altText: 'HTML Logo'
    },
    {
      name: 'CSS',
      logo: 'CSSLogo.png',
      altText: 'CSS Logo'
    }
  ]},
project2: {
  otherData: 'otherInfo',
  technology: [ 
    {
      name: 'HTML',
      logo: 'logoTest.png',
      altText: 'HTML Logo'
    },
    {
      name: 'CSS',
      logo: 'CSSLogo.png',
      altText: 'CSS Logo'
    },
    {
      name: 'JavaScript',
      logo: 'jsTest.png',
      altText: 'JavaScript Logo'
    }
  ]}

//Component
const Technology = props => {
  const techList = props.technology; //object
  console.log(techList); //logs: [{...},{name: 'CSS',logo: 'CSSLogo.png',altText:'CSS Logo'}]
   let techItem = techList.forEach(function(arrayItem) {
      let name = arrayItem.name;
      let logo = arrayItem.logo;
      let altText = arrayItem.altText;
      console.log(name, logo, altText); //logs: CSS CSSLogo.png CSSLogo

        let techDetails = [name, logo, altText];
        console.log(techDetails); //logs values in separate console.logs: name, logo, altText

        let loopThru = techDetails.forEach(function(item) {
            console.log(item);
            return item;
        });

      console.log(techItem,loopThru); //logs 'undefined, 'undefined'
    });

    return(
        <div>
          <p>Tech Component</p> //rendered in browser
          <p>{loopThru}</p> // is NOT rendered in browser
        </div>
    );
}; 

I am attempting to render out prop values from a nested object. I'm running into two issues: 1. JSX line of code (containing variables) is not rendering in the browser 2. console logging the variables, containing the .forEach() methods, return 'undefined'

Question: How can I access the nested prop values, pass them into the JSX code, and render in the browser?

Current state of my code:

//data object
project1: {
  otherData: 'value',
  technology: [ 
    {
      name: 'HTML',
      logo: 'logoTest.png',
      altText: 'HTML Logo'
    },
    {
      name: 'CSS',
      logo: 'CSSLogo.png',
      altText: 'CSS Logo'
    }
  ]},
project2: {
  otherData: 'otherInfo',
  technology: [ 
    {
      name: 'HTML',
      logo: 'logoTest.png',
      altText: 'HTML Logo'
    },
    {
      name: 'CSS',
      logo: 'CSSLogo.png',
      altText: 'CSS Logo'
    },
    {
      name: 'JavaScript',
      logo: 'jsTest.png',
      altText: 'JavaScript Logo'
    }
  ]}

//Component
const Technology = props => {
  const techList = props.technology; //object
  console.log(techList); //logs: [{...},{name: 'CSS',logo: 'CSSLogo.png',altText:'CSS Logo'}]
   let techItem = techList.forEach(function(arrayItem) {
      let name = arrayItem.name;
      let logo = arrayItem.logo;
      let altText = arrayItem.altText;
      console.log(name, logo, altText); //logs: CSS CSSLogo.png CSSLogo

        let techDetails = [name, logo, altText];
        console.log(techDetails); //logs values in separate console.logs: name, logo, altText

        let loopThru = techDetails.forEach(function(item) {
            console.log(item);
            return item;
        });

      console.log(techItem,loopThru); //logs 'undefined, 'undefined'
    });

    return(
        <div>
          <p>Tech Component</p> //rendered in browser
          <p>{loopThru}</p> // is NOT rendered in browser
        </div>
    );
}; 
Share edited Jun 28, 2018 at 20:09 Code-Apprentice 83.6k26 gold badges158 silver badges284 bronze badges asked Jun 28, 2018 at 19:56 JSzeto821JSzeto821 551 gold badge1 silver badge8 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

Your loopThru variable was never assigned anything because forEach() always returns undefined. You probably meant to use map() as that actually returns an array which you can render.

From MDN:

forEach() executes the callback function once for each array element; unlike map() or reduce() it always returns the value undefined and is not chainable.

So instead do:

let loopThru = techDetails.map(function(item) {
  console.log(item);
  return item;
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742404694a4437608.html

相关推荐

  • javascript - React - result of forEach() is undefined - Stack Overflow

    I am attempting to render out prop values from a nested object. I'm running into two issues: 1. JS

    4小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信