【问题标题】:Reducing redundant information in a JSON object with javascript and combining values使用 javascript 减少 JSON 对象中的冗余信息并组合值
【发布时间】:2020-08-07 03:44:09
【问题描述】:

我有一个结果的 CSV,它查看图片并猜测图片是否包含某个属性。在这种情况下,如果图片中的主题是男性或女性。

我正在使用 javascript/node 将此 CSV 转换为 JSON,我想获取属性及其值并将它们放入每个 pciture 一个对象内的数组中。现在,CSV 度量和属性的每一行,但这意味着每个图像至少有两行。

简单版的csv:

path, detect_id, score, x-coord, y-coord, w-coord, h-coord, attribute, value

picture_1.jpg,0,1.44855535,74,54,181,181,genderf,0.024716798
picture_1.jpg,0,1.44855535,74,54,181,181,genderm,0.975283206

我可以将此 CSV 转换为 JSON,然后至少按它们的路径/文件名将项目组合在一起。

但这会留下很多冗余信息,我想将我的属性和它们的值放在主对象内的嵌套对象中。

喜欢:

Path: picture_1.jpg
Attributes: [genderf: 0.025, 
             genderm: 0.985]
other_info: other info

现在我正在使用 lodash 创建如下所示的对象,但是如果我尝试映射属性,我最终会推出除最后一个元素之外的所有元素。

所以我可以使用以下代码创建对象。

var result = 
    _([...arr1, ...arr2])
    .concat()
    .groupBy("path")
    .value();

其中 arr1 和 arr2 是来自输出 csv 的一行的数据。除了属性及其值之外,所有信息都是相同的。

这让我得到了这个对象:

{
  "picture_1.jpg": [
    {
      "path": "picture_1.jpg",
      "detect_id,": "0",
      "score,": "1.44855535",
      "coordinates": [
        {
          "x,": "74",
          "y,": "54",
          "w": "181",
          "h": "181"
        }
      ],
      "attribute": "genderf",
      "value": "0.024716798"
    },
    {
      "path": "picture_1.jpg",
      "detect_id,": "0",
      "score,": "1.44855535",
      "coordinates": [
        {
          "x,": "74",
          "y,": "54",
          "w": "181",
          "h": "181"
        }
      ],
      "attribute": "genderm",
      "value": "0.975283206"
    }
  ]
}

这至少根据路径标题将图片组合在一起,但很多信息是多余的,这只是衡量一个属性。

【问题讨论】:

    标签: javascript node.js json csv lodash


    【解决方案1】:

    您可以迭代所有csv-行并构建object/map,同时跟踪已找到的文件名/路径。如果遇到地图中已存在路径的线路,只需附加 attribute/value 对。像这样的东西(请注意,为了简单起见,我更改了坐标分隔符,并且需要适当的错误处理):

        const data = ["picture_1.jpg,0,1.44855535,74;54;181;181,genderf,0.024716798", "picture_1.jpg,0,1.44855535,74;54;181;181,genderm,0.975283206"];
        
            function createImageDataMap(dataArr) {
                const imageDataResult = {};
            
                for (const imgData of dataArr) {
                    const currData = parseImgDataLine(imgData);
                    if (!imageDataResult[currData.path]) {
                        imageDataResult[currData.path] = {
                            attributes: [], other_info: {
                                score: currData.score,
                                detectId: currData.detectId,
                                coords: currData.coords
                            }
                        }
            
                    }
                    imageDataResult[currData.path].attributes.push({[currData.attribute]: currData.value});
                }
            
                return imageDataResult;
            
            }
            
            function parseImgDataLine(line) {
                const attributes = line.split(',');
                return {
                    path: attributes[0],
                    detectId: attributes[1],
                    score: attributes[2],
                    coords: attributes[3],
                    attribute: attributes[4],
                    value: attributes[5]
                }
            }
            
            console.log(JSON.stringify(createImageDataMap(data)));
        // prints {"picture_1.jpg":{"attributes":[{"genderf":"0.024716798"},{"genderm":"0.975283206"}],"other_info":{"score":"1.44855535","detectId":"0","coords":"74;54;181;181"}}}
    

    【讨论】:

    • 哇,这真是太棒了。我可以使用它来处理更大的 csv,但我已将具有属性的不同文件名添加到数据 Const 中,我可以使用该 json 对象并将其插入到我正在使用的数据库中。谢谢!
    • 太棒了! :) 不要忘记接受我的回答,谢谢!
    猜你喜欢
    • 2021-12-12
    • 2014-08-28
    • 1970-01-01
    • 2017-12-01
    • 1970-01-01
    • 2019-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多