【问题标题】:Vue How to Merge Two Arrays From Same Object?Vue如何合并来自同一个对象的两个数组?
【发布时间】:2019-03-25 20:15:56
【问题描述】:

我从一个 API 中提取数据,该 API 为我提供了一个项目对象,每个项目都包含一个名为 correct_answer 的字符串和一个名为 incorrect_answers 的数组。

我试图在每个项目中合并这些值,所以当我执行v-for 时,它会循环遍历合并在一个循环中的答案。我还想随机化这些答案的顺序。

对象外观示例:

"results": [
    {
      "question": "Where was the Games of the XXII Olympiad held?",
      "correct_answer": "Moscow",
      "incorrect_answers": [
        "Barcelona",
        "Tokyo",
        "Los Angeles"
      ]
    },
    {
      "question": "What is the first weapon you acquire in Half-Life?",
      "correct_answer": "A crowbar",
      "incorrect_answers": [
        "A pistol",
        "The H.E.V suit",
        "Your fists"
      ]
    },
]

【问题讨论】:

    标签: javascript arrays vue.js vuejs2


    【解决方案1】:

    var res = [
        {
          "question": "Where was the Games of the XXII Olympiad held?",
          "correct_answer": "Moscow",
          "incorrect_answers": [
            "Barcelona",
            "Tokyo",
            "Los Angeles"
          ]
        },
        {
          "question": "What is the first weapon you acquire in Half-Life?",
          "correct_answer": "A crowbar",
          "incorrect_answers": [
            "A pistol",
            "The H.E.V suit",
            "Your fists"
          ]
        },
    ]
    
    var answers = res.map(i => [i.correct_answer, ...i.incorrect_answers])
    
    console.log(answers[0])
    console.log(answers[1])

    【讨论】:

      【解决方案2】:

      您可以将 1️⃣spreadincorrect_answers 放入带有 correct_answer 的新数组中,然后 2️⃣ 将结果作为新属性附加到原始数据上。另外,3️⃣你可以shuffle答案,而你在它:

      const newData = data.map(x => {
        const answers = [x.correct_answer, ...x.incorrect_answers] /*1️⃣*/
        x.answers /*2️⃣*/ = shuffle(answers) /*3️⃣*/
        return x
      })
      

      这个新数组可以在您的模板中用作computed property(例如,命名为questions):

      <fieldset v-for="q in questions" :key="q.question">
      

      const rawData = {
        "results": [
          {
            "question": "Where was the Games of the XXII Olympiad held?",
            "correct_answer": "Moscow",
            "incorrect_answers": [
              "Barcelona",
              "Tokyo",
              "Los Angeles"
            ]
          },
          {
            "question": "What is the first weapon you acquire in Half-Life?",
            "correct_answer": "A crowbar",
            "incorrect_answers": [
              "A pistol",
              "The H.E.V suit",
              "Your fists"
            ]
          },
        ]
      }
      
      new Vue({
        el: '#app',
        data() {
          return {
            rawData: rawData.results
          }
        },
        computed: {
          questions() {
            return this.rawData.map(x => {
              const answers = [x.correct_answer, ...x.incorrect_answers]
              x.answers = shuffle(answers)
              this.$set(x, 'answer', null) // create reactive answer prop
              return x
            })
          }
        }
      })
      
      // https://stackoverflow.com/a/2450976/6277151
      function shuffle(array) {
        let currentIndex = array.length, temporaryValue, randomIndex;
      
        // While there remain elements to shuffle...
        while (0 !== currentIndex) {
      
          // Pick a remaining element...
          randomIndex = Math.floor(Math.random() * currentIndex);
          currentIndex -= 1;
      
          // And swap it with the current element.
          temporaryValue = array[currentIndex];
          array[currentIndex] = array[randomIndex];
          array[randomIndex] = temporaryValue;
        }
      
        return array;
      }
      <script src="https://unpkg.com/vue@2.6.10"></script>
      
      <div id="app">
        <form action="#">
          <fieldset v-for="(q,i) in questions" :key="q.question">
            <h2>{{q.question}} {{q.answer}}</h2>
            
            <div v-for="a in q.answers" :key="a">
              <label>
                <input type="radio" :name="i" :value="a" @change="q.answer = a">
                <span>{{a}}</span>
              </label>
            </div>
          </fieldset>
        </form>
      </div>

      【讨论】:

      • 谢谢!这就是我一直在寻找的。对此不太熟悉。$set,因此将对此进行更多研究。
      • @user9664977 没问题 :)
      【解决方案3】:

      let merged = [item.correct_answer, ...item.incorrect_answers],还是我错过了什么?

      【讨论】:

      • 这不会像你期望的那样工作:[[1,2], ...[3,4]][[1,2],3,4]
      • 在 OP 中,correct_answer 不是一个数组,而是一个值。
      • @mbojko 是的,你是对的,当我说两个数组时,我说错了,它将是一个数组和一个值。而且我认为我要挂断的事情是我需要将其作为计算属性来执行吗?然后对该计算属性的结果执行 v-for 循环?
      猜你喜欢
      • 2020-02-04
      • 1970-01-01
      • 1970-01-01
      • 2018-04-08
      • 1970-01-01
      • 1970-01-01
      • 2023-01-14
      • 2020-10-12
      • 1970-01-01
      相关资源
      最近更新 更多