【问题标题】:Transforming object of objects in React在 React 中转换对象的对象
【发布时间】:2018-06-17 13:33:57
【问题描述】:

我对 JS 和 React 还很陌生,但遇到以下问题(也许很简单,但我不知道如何解决)。

这是我拥有的对象:

{
  React: {
    title: 'React',
    questions: [
      {
        question: 'What is React?',
        answer: 'A library for managing user interfaces'
      },
      {
        question: 'Where do you make Ajax requests in React?',
        answer: 'The componentDidMount lifecycle event'
      }
    ]
  },
  JavaScript: {
    title: 'JavaScript',
    questions: [
      {
        question: 'What is a closure?',
        answer: 'The combination of a function and the lexical environment within which that function was declared.'
      }
    ]
  }
}

这就是我需要的:

[
  { title: "React", questions: 2},
  { title: "JavaScript", questions: 1}
]

我已经尝试过Object.keys,然后对其进行映射 - 这给了我一个新数组中的标题或问题。

【问题讨论】:

    标签: javascript reactjs react-native ecmascript-6


    【解决方案1】:

    您可以映射Object.values 并提取您需要的值。

    const data = {
      React: {
        title: 'React',
        questions: [{
            question: 'What is React?',
            answer: 'A library for managing user interfaces'
          },
          {
            question: 'Where do you make Ajax requests in React?',
            answer: 'The componentDidMount lifecycle event'
          }
        ]
      },
      JavaScript: {
        title: 'JavaScript',
        questions: [{
          question: 'What is a closure?',
          answer: 'The combination of a function and the lexical environment within which that function was declared.'
        }]
      }
    }
    
    console.log(
      Object.values(data).map(({
        title,
        questions: {
          length: questions
        }
      }) => ({
        title,
        questions
      }))
    )

    【讨论】:

      【解决方案2】:

      Object.keys 是要走的路。

      const data = {
        React: {
          title: 'React',
          questions: [
            {
              question: 'What is React?',
              answer: 'A library for managing user interfaces'
            },
            {
              question: 'Where do you make Ajax requests in React?',
              answer: 'The componentDidMount lifecycle event'
            }
          ]
        },
        JavaScript: {
          title: 'JavaScript',
          questions: [
            {
              question: 'What is a closure?',
              answer: 'The combination of a function and the lexical environment within which that function was declared.'
            }
          ]
        }
      };
      
      const newArray = Object.keys(data).map((key) => {
        return {
          title: data[key].title,
          questions: data[key].questions.length
        }
      });
      
      console.log(newArray)

      【讨论】:

        【解决方案3】:

        使用lodash的reduce更加清晰简单,因为它可以像数组一样迭代对象:

        import { reduce } from 'lodash';
        
        const newData = reduce(
          data,
          (acc, { title, questions }) => [
            ...acc,
            { title, questions: questions.length },
          ],
          []
        );
        

        【讨论】:

          猜你喜欢
          • 2022-12-17
          • 2021-01-27
          • 1970-01-01
          • 1970-01-01
          • 2021-10-20
          • 2017-01-11
          • 2019-04-10
          • 2017-05-20
          • 2020-06-15
          相关资源
          最近更新 更多