Programing

두 어레이를 반복하고 데이터 결합

c10106 2022. 4. 11. 21:19
반응형

두 어레이를 반복하고 데이터 결합

나는 무언가를 할 수 있는 가장 좋은 방법을 찾으려고 노력하고 있다.기본적으로 CSV를 업로드할 수 있도록 허용한다.이 경우, 나는 예를 들어, 업로드 상태를 배열 내에 저장한다.

tempAttachments:Array[2]
  0:Object
    _id:"12345-678910"
    bytes:412051
    file:File
    size:411532
    title:"someFile.csv"
    type:"file"
    fileType:"typeOne"
  1:Object
    _id:"9999-2222"
    bytes:12345
    file:File
    size:23456
    title:"anotherFile.csv"
    type:"file"
    fileType:"typeTwo"

이 파일들이 확인되고 제출되면, 나는 처음 5행은 그들의 데이터를 반환하는 것이라고 읽었다.그래서 나는 어떤 라인이 어떤 파일에 속하는지 알고, 나는 키를 그 파일에 맞추었다.fileType.

반환되는 데이터는 이와 같이 보인다.

data:
  0: 
    typeOne: 
      0: ["123", "21/11/2013", "10"]
      1: ["234", "22/11/2013", "20"]
      2: ["345", "12/09/2018", "100"]
      3: ["456", "22/03/2016", "350"]
      4: ["567", "13/02/2013", "50"]

이것이 typeOne 파일이 있는 파일의 처음 5개 행입니다.지금 하려는 작업은 이 어레이를 의 appropiate 어레이 요소에 추가하는 것이다.tempAttachments.

그래서 나는 데이터 배열을 담당하는 내 기능을 가지고 있다.그런 다음, 첨부 파일 배열을 반복해야 하며, 이 루프 안에 데이터 배열을 반복해야 한다고 가정한다.그럼 열쇠를 가져와야겠군. 그리고 이걸 돌려서 일치하는 게 있는지 봐야지.지금까지 내가 가지고 있는 것은 이것이다.

createFileData (state, data) {
  for (const attachment of state.tempAttachments) {
    for (const fileData of data) {
      for (const key of Object.keys(fileData)) {
        if (key === attachment.fileType) {
          console.log('Match')
        }
      }
    }
  }
}  

네가 볼 수 있듯이, 그것은 매우 친절하지 않다.그래서 제 질문은 ES6를 이용해서 좀 더 우아하게 할 수 있을까 하는 겁니다.또한 데이터를 첨부 파일 배열에 추가하는 가장 좋은 방법은 무엇인가?나는 스프레드 오퍼레이터와 놀아보았지만 아직 완전히 파악하지 못했다.

고마워요.

아마 이렇게 해 볼 수 있을 것이다.

createFileData (state, data) 
{
  const types = {}; // our hashmap

  // collect all unique types
  data.forEach(item =>
  {
    Object.keys(item).forEach(key =>
    {
      types[key] = true;
    });
  });

  state.tempAttachments.forEach(attachment =>
  {
    if (attachment.fileType in types)
    {
      console.log('Match')
    }
  });
}

새 데이터 추가

createFileData (state, data) 
{
  data.forEach(item =>
  {
    Object.entries(item).forEach((type, rows) =>
    {
      rows.forEach(row =>
      {
        state.tempAttachments.push({
          fileType: type,
          documentID: row[0],
          documentDate: row[1],
          documentAmount: row[2],
        });
      });
    });
  });
}

만약 당신이 그것을 시도하고 싶다면 여기 다른 접근법이 있다.

  tempAttachments=[
  {
    _id:"12345-678910",
    bytes:412051,
    file:'File',
    size:411532,
    title:"someFile.csv",
    type:"file",
    fileType:"typeOne",
  },
  {
    _id:"9999-2222",
    bytes:12345,
    file:'File',
    size:23456,
    title:"anotherFile.csv",
    type:"file",
    fileType:"typeTwo"
  }
  ]
  data={
typeOne:{
  0: ["123", "21/11/2013", "10"],
  1: ["234", "22/11/2013", "20"],
  2: ["345", "12/09/2018", "100"],
  3: ["456", "22/03/2016", "350"],
  4: ["567", "13/02/2013", "50"]
 }
} 

     
tempAttachments.forEach(o=>{Object.entries(data).forEach(y=>{ if(o.fileType==y[0]) Object.assign(o,{[y[0]]:y})})})
console.log(tempAttachments)

참조URL: https://stackoverflow.com/questions/62535156/looping-two-arrays-and-combining-data

반응형