简体   繁体   English

如何在元素条件下将一个数组拆分为两个 arrays

[英]How can I can split one array into two arrays with condition on element

I'm trying to display a graph with plotly.js.我正在尝试使用 plotly.js 显示图表。 For this, I have an array called data, which contains all my data.为此,我有一个名为 data 的数组,其中包含我的所有数据。 But I want to display only selected elements/datas by the user.但我只想显示用户选择的元素/数据。 So, I want to have two arrays with displayed elements and deleted elements.所以,我想要两个 arrays 显示元素和删除元素。
My project works with vuejs and plotly.js我的项目使用 vuejs 和 plotly.js

Here is my code:这是我的代码:

//clusterCheckboxes are my checkboxes, they are all selected by default
const clusterCheckboxes = document.querySelectorAll("input[type='checkbox'].cluster")
      //My newData contains the displayed datas
      this.newData = []
      //deletedLines containes not displayed data
      this.deletedLines = []

      
      for (let i = 0; i < clusterCheckboxes.length; i++) {
        for(let j = 0; j < toRaw(this.data).length; j++){
          //Si le checkbox est cochée on ajoute les lignes correspondantes à newData
          if(clusterCheckboxes[i].checked){
            this.newData.push(toRaw(this.data)[j])
          }else{
            this.deletedLines.push(toRaw(this.data)[j])
          }
        }
      }
      //This lines create a new plotly graph
      Plotly.newPlot('myDiv', this.newData);```

I am assuming clusterCheckboxes and data share the same length, then you can use the same index and split them based on the condition.我假设clusterCheckboxesdata共享相同的长度,那么您可以使用相同的索引并根据条件拆分它们。

//clusterCheckboxes are my checkboxes, they are all selected by default
const clusterCheckboxes = 
document.querySelectorAll("input[type='checkbox'].cluster")
  //My newData contains the displayed datas
  this.newData = []
  //deletedLines containes not displayed data
  this.deletedLines = []

  
    for(let j = 0; j < toRaw(this.data).length; j++){
      //Si le checkbox est cochée on ajoute les lignes correspondantes à 
newData
      if(clusterCheckboxes[j].checked){
        this.newData.push(toRaw(this.data)[j])
      }else{
        this.deletedLines.push(toRaw(this.data)[j])
      }
    }
  //This lines create a new plotly graph
  Plotly.newPlot('myDiv', this.newData);

Assuming your checkboxes and data are the same length, you can just use filter to separate the data:假设您的复选框和数据的长度相同,您可以使用filter来分隔数据:

const clusterCheckboxes = document.querySelectorAll("input[type='checkbox'].cluster")

const rawData = toRaw(this.data)

this.newData = rawData.filter((_, i) => clusterCheckboxes[i].checked)

Plotly.newPlot('myDiv', this.newData)

If you really want the deletedLines value you can generate it the same way:如果你真的想要deletedLines值,你可以用同样的方式生成它:

this.deletedLines = rawData.filter((_, i) => !clusterCheckboxes[i].checked)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将数组拆分为具有交替元素的两个数组 - How can I split an array into two arrays with alternating elements 我如何创建一个数组,两个数组,一个从 0 到 100,一个是第一个数组的运行总和? - how can i create an array two arrays, one from 0 to 100 and one that is the running sum of the first array? 如何在特定条件下混洗两个 arrays 的元素? - How can I shuffle the elements of two arrays with a certain condition? 如何在有条件的情况下加入两个不同的 arrays? - How can I join two different arrays with condition? 如何在一个数组元素中拆分两个数据 - How to split two data in one array element 如何使用javascript或angular将一个数组的每个元素添加到其他数组中的对应元素 - How can I add each element of one array to corresponding elements in other arrays with javascript or angular 如何评估数组中的每个元素并在一个条件下使用 useState()? - How can I evaluate each element in array and use useState() on one condition? 当一个数组是二维的时,如何将两个 arrays 对象合并在一起? - How can I merge two arrays of objects together when one array is 2D? 如何将一列的 html5 表分成两列? - How can I split an html5 table of one column into two? 如何在数组数组中找到元素的索引? - How can I find the index of an element, in an array of arrays?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM