JavaScript How to loop through properties of objects inside of an array within a React component? loop through properties javascript,loop through properties of object c#,

Here is my Parent component's render function:

  render() {
    const users = [
      'tom': {
        phone: '123',
        email: 'hotmail'
      },
      'rob': {
        phone: '321',
        email: 'yahoo'
      },
      'bob': {
        phone: '333',
        email: 'gmail'
      },
    ];

    const list = users.map((user) =>
      (<User
        name={user}
        phone={users.phone}
        email={users.email}
      />),
    );

    return <ul>{list}</ul>;
  }

The output shows up like this:

enter image description here

And here is the Child component's render function:

  render() {
    const {
      name,
      phone,
      email,
    } = this.props;

    const info = [name, phone, email];

    const item = info.map((index) => (
      <li key={index}>
        { index }
      </li>
    ));

    return item;
  }

How can I get it to show with the phone numbers and emails? Not sure what I'm doing incorrectly. Thanks.

Answer:1

At first this is not valid javascript:

const users = [
  'tom': {
    phone: '123',
    email: 'hotmail'
  },
  // ...
];

You should either define an array or an object. Let's say your users is an object literal:

const users = {
  'tom': {
    phone: '123',
    email: 'hotmail'
  },
  // ...
};

Then you can iterate over entries of the object:

const list = Object.entries(users).map(([name, info]) =>
  (<User
    name={name}
    phone={info.phone}
    email={info.email}
  />)
);

However Object.entries() is not supported in all browsers, check if it works in your environment.

Answer:2

First thing is that users is not a valid array If you want key value pair in main scope then use Object({})

 render() {
        const users = {
          'tom': {
            phone: '123',
            email: 'hotmail'
          },
          'rob': {
            phone: '321',
            email: 'yahoo'
          },
          'bob': {
            phone: '333',
            email: 'gmail'
          },
        };

        const list = Object.keys(users).map((user) =>
          (<User
            name={user}
            phone={users[user].phone}
            email={users[user].email}
          />),
        );

        return <ul>{list}</ul>;
      }
Answer:3

You need to be referring to "user" instead of "users" inside of the Parent component's map function. The purpose of the "user" variable is to represent the current instance of each element in the array. So the map function should look like:

const list = users.map((user) =>
  (<User
    name={user}
    phone={user.phone}
    email={user.email}
  />),
);

instead.

Answer:4

One solution is to change your "list" to an actual list:

const users = [
  {
    name: 'tom',
    phone: '123',
    email: 'hotmail'
  },
  {
    name: 'rob,
    phone: '321',
    email: 'yahoo'
  },
  {
    name: 'bob',
    phone: '333',
    email: 'gmail'
  },
];

Now user user.name instead of user.

Alternatively, create an object keyed by the names of each user:

const users = {
  'tom': {
    phone: '123',
    email: 'hotmail'
  },
  'rob': {
    phone: '321',
    email: 'yahoo'
  },
  'bob': {
    phone: '333',
    email: 'gmail'
  },
};

Then map over the keys:

const list = Object.keys(users).map((user) =>
  (<User
    name={user}
    phone={users[user].phone}
    email={users[user].email}
  />),
);
Answer:5

I have following parent route in my app: .state('app',{ url: '/app', templateUrl: 'views/app.html', resolve: loadSequence('modernizr','moment'), ...

I have following parent route in my app: .state('app',{ url: '/app', templateUrl: 'views/app.html', resolve: loadSequence('modernizr','moment'), ...

  1. angularjs router resolve
  2. angularjs router resolve reject
  3. angularjs router resolve service
  4. angularjs router resolve not working
  5. angularjs router resolve data
  6. angularjs router resolve factory
  7. angularjs routing resolve
  8. angularjs routing resolve example
  9. angularjs routing resolve service
  10. angularjs ui router resolve
  11. angularjs ui router resolve redirect
  12. angularjs ui router resolve promise
  13. angularjs ui router resolve example
  14. angularjs ui router resolve not working
  15. angularjs ui-router resolve unknown provider
  16. angularjs ui router resolve authentication example
  17. angularjs router state resolve
  18. angularjs ui-router resolve parameter
  19. angularjs ui router resolve authentication
  20. angularjs ui router resolve data

Im trying to use http://www.chartjs.org/ to create a pie chart while retrieve database values. Following are my codes callactiveinactive.php <?php require connectDB.php; $sql = "SELECT ...

Im trying to use http://www.chartjs.org/ to create a pie chart while retrieve database values. Following are my codes callactiveinactive.php <?php require connectDB.php; $sql = "SELECT ...

i have tried to perform axios.delete operation in react, unfortunately it is not working, please help me to solve this issue, export function DeletePatient(token,deletePatient) { return axios....

i have tried to perform axios.delete operation in react, unfortunately it is not working, please help me to solve this issue, export function DeletePatient(token,deletePatient) { return axios....

  1. axios delete method not allowed
  2. axios delete method body
  3. axios delete method
  4. axios delete request method options
  5. laravel axios delete method not allowed
  6. axios delete 405 method not allowed
  7. axios js delete method

i get weather json from forecast.io and in json data forecast.io have timestamp : time:1445767494,"summary":"صافِ","icon":"clear-day","precipIntensity":0,"precipProbability":0,"temperature":32.99,"...

i get weather json from forecast.io and in json data forecast.io have timestamp : time:1445767494,"summary":"صافِ","icon":"clear-day","precipIntensity":0,"precipProbability":0,"temperature":32.99,"...