实际 json 值与使用 javascript / node js 的预期值差异更大

Real json value is more different then expected value using javascript / node js

我正在破坏函数中的数据

let { destructValue } = require("./destructValue.js");
const fs = require("fs");
const path = require("path");

function retrunValues(data) {
  fs.readdir("./userData", (err, files) => {
    if (err) throw console.log(err.message);
    else {
      files.forEach(async (file) => {
        fs.readFile(`./userData/${file}`, (err, data) => {
          destructValue(data);
          let jsonObject = {};
          if (destructValue(data).length !== 0) {
            jsonObject = {
              name: data.name,
              value: [...destructValue(data)],
            };
            console.log(jsonObject);
          }
        });
      });
    }
  });
}

所以在执行 console.log(jsonObject) 之后我得到了这个值

{
 name: "Tomas",
 value:[{
  age: "21",
  address: "New York"
 }]
}
{
 name: "Jerry",
 value:[{
  age: "22",
  address: "Tokyo"
 }]
}

这个输出对我来说很好,因为这是预期的输出,所以我尝试像这样将 jsonObject 调用到新模式中

let { destructValue } = require("./destructValue.js");
const fs = require("fs");
const path = require("path");

function retrunValues(data) {
  fs.readdir("./userData", (err, files) => {
    if (err) throw console.log(err.message);
    else {
      files.forEach(async (file) => {
        fs.readFile(`./userData/${file}`, (err, data) => {
          destructValue(data);
          let jsonObject = {},
            testingObject = {};
          if (destructValue(data).length !== 0) {
            jsonObject = {
              name: data.name,
              value: [...destructValue(data)],
            };
          }

          testingObject = {
            default: "default value",
            got: jsonObject,
          };

          console.log(testingObject);
        });
      });
    }
  });
}

所以在执行 console.log(testingObject) 之后我得到了这个值

{ default: 'defaultObject'}
{ default: 'defaultObject'}
{ default: 'defaultObject',
  name: "Tomas",
  value:[{
   age: "21",
   address: "New York"
  }]
}
{ default: 'defaultObject'}
{ default: 'defaultObject'}
{ default: 'defaultObject',
  name: "Jerry",
  value:[{
   age: "22",
   address: "Tokyo"
  }]
}

因为我没想到这个输出,因为我的预期输出是这样的

[
  {
    "default": "defaultObject",
    "got": [
      {
        "name": "Tomas",
        "value": [{ "age": "21", "address": "New York" }]
      },
      {
        "name": "Jerry",
        "value": [{ "age": "22", "address": "Tokyo" }]
      }
    ]
  }
]

但是我没有得到预期的价值,我不知道哪里出了问题我怎样才能达到我的预期价值

destructValue.js

const {age,address}=require(valueGot.js);

function destructValue(data){
 const destructSchema=[];
 for(const value of data){
  switch(value.userDetails){
   case "age":
    destructSchema.push(age(data));
   break;
   case "address":
    destructSchema.push(address(data));
   break;
  }
 }
return destructSchema;
}

module.exports={destructValue}

尝试更改 requrie

let {destructValue} = requrie("./destructeValue.js");

要求

let {destructValue} = require("./destructeValue.js");

试试下面的方法。将结果保存到一个数组中,当您完成文件处理后,输出结果。

编辑: 我为结果添加了一个过滤器,但还没有测试。它应该只添加之前未添加的已破坏数据。

let { destructValue } = require("./destructValue.js");
const fs = require("fs");
const path = require("path");

function filterAgainstExisting(existing, check) {
  return check.reduce((carry, value, index, array) => {
    //Check if value exists - not generic, catered to your needs
    if (existing[value.name] && existing[value.name].age == value.value[0].age && existing[value.name].address == value.value[0].address) {
      return carry;
    }
    
    existing[value.name] = value;
    carry.push(value);
    return carry;
  }, []);
}

function retrunValues(data) {
  fs.readdir("./userData", (err, files) => {
    if (err) throw console.log(err.message);
    else {
      let jsonArray = [];
      let jsonObject = {};
      files.forEach(async (file) => {
        fs.readFile(`./userData/${file}`, (err, data) => {
          let destructedData = destructValue(data);
          if (destructedData.length !== 0) {
            jsonArray.push({
              name: data.name,
              value: [...this.filterAgainstExisting(jsonObject, destructedData)],
            });
          }
        });
      });

      const testingObject = [{
        default: "defaultObject",
        got: jsonArray,
      }];

      console.log(testingObject);
    }
  });
}