繁体   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