簡體   English   中英

循環遍歷對象數組,以在嵌套數組中提取數據

[英]Loop through array of objects, to extract data within nested arrays

示例數據集

const data = [
        {
        location: "1A",
        uId: 1,
        notNeededData: null,
        components: [
          {
            modelId: "7654",
            partNumber: "P1",
            description: "It's a desk.",
            notNeededData: null,
            location: "office1"
          },
          {
            modelId: "1234",
            part: "P2",
            description: "It's a chair",
            notNeededData: null,
            location: "office1"
          }
        ]
      },
      {
        location: "2B",
        uKeyId: 1,
        notNeededData: null,
        components: [
          {
            modelId: "9876",
            partNumber: "P8",
            description: "The best headrest",
            notNeededData: null,
            location: "office2"
          },
          {
            modelId: "7463",
            partNumber: "P5",
            description: "The stool",
            notNeededData: null,
            location: "office2"
          }
        ]
      }
    ];

期望的結果集為一個新的對象數組,如下:

         [
          {
            id:1,
            uId: 1,
            location: "1A",
            modelId: "7654",
            partNumber: "P1",
            description: "It's a desk."
          },
          {
            id:2,
            uId:1,
            location: "1A",
            modelId: "1234",
            part: "P2",
            description: "It's a chair"
          },
          {
            id:3,
            uId: 2,
            location: "2B",
            modelId: "9876",
            partNumber: "P8",
            description: "The best headrest"
          },
          {
            id:4,
            uId: 2,
            location: "2B",
            modelId: "7463",
            partNumber: "P5",
            description: "The stool"
          }
        ]

我嘗試使用以下函數遍歷數組,但我只成功復制了幾個值集。

const getNewDataSet = (d) => {
      let newArr = [];
    
      for (let i = 0; i < d.length; i++) {
        let obj = {};
        obj["id"] = i + 1;
        obj["uId"] = d[i].uId;
        obj["location"] = d[i].location;
        for (let k = 0; k < d[i].components.length; k++) {
          obj["modelId"] = d[i].components[k].modelId;
          obj["partNumber"] = d[i].components[k].partNumber;
          obj["description"] = d[i].components[k].description;
          newArr.push(obj);
        }
      }
      return newArr;
    };

請讓我知道是否需要任何其他信息,或者我可能遺漏的任何信息。

非常感謝,謝謝。

那是我的解決方案,在我看來,不要使用很多索引以免混淆,所以我在循環組件時使用 forEach() 來輕松選擇每個組件,並且您還需要在推送后再次清空對象它從其中清除所有舊數據,並且我聲明了一個變量(id)來為每個組件提供一個唯一 ID,並在循環的每個循環中將其遞增為 [i] 在這種情況下它不是唯一的,因為其中有多個組件輸入數據的相同對象。

        const getNewDataSet = (data) => {
          let newArr = [];
          let obj = {};
          let id = 1;
          for (let i = 0; i < data.length; i++) {
              const currObj = data[i];
              currObj.components.forEach((comp) => {
                   obj["id"] = id;
                   obj["uId"] = currObj.uId || currObj.uKeyId;
                   obj["location"] = data[i].location;
                   obj["modelId"] = comp.modelId;
                   obj["partNumber"] = comp.partNumber;
                   obj["description"] = comp.description;
            newArr.push(obj);
            obj = {};
            id++;
        })
   }
   return newArr;
 };

也許有更好的解決方案,但它正在工作。 我希望這可以幫助你

 const data = [{ location: "1A", uId: 1, notNeededData: null, components: [{ modelId: "7654", partNumber: "P1", description: "It's a desk.", notNeededData: null, location: "office1" }, { modelId: "1234", part: "P2", description: "It's a chair", notNeededData: null, location: "office1" } ] }, { location: "2B", uKeyId: 1, notNeededData: null, components: [{ modelId: "9876", partNumber: "P8", description: "The best headrest", notNeededData: null, location: "office2" }, { modelId: "7463", partNumber: "P5", description: "The stool", notNeededData: null, location: "office2" } ] } ]; const formatRespnse = (data) => { let records = [], mainComponents = [] i = 1; data.forEach((record) => { let components = {}; let newData = {}; record.components.forEach((component) => { newData = { id: i, uId: record.uId, location: record.location, modelId: component.modelId, partNumber: component.partNumber || component.part, description: component.description, } records.push(newData); i++; }); }); return records; } console.log(formatRespnse(data));

您可以使用 map 函數來簡化數組循環,如下所示:

const extractData = data => {
  let id = 1;

  return data.map(d => {
    return d.components.map(dd => {
      const newObj = {
        id: id,
        uId: d.uId || d.uKeyId,
        location: d.location,
        modelId: dd.modelId,
        partNumber: dd.partNumber || dd.part,
        description: dd.description
      }
      id += 1;
      return newObj;
    });
  });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM