通过使用 javascript / node js 匹配另一个 json 的 json 键和值来分配值

assign value by matching the json key and value of another json using javascript / node js

我试图通过深入比较嵌套的 json 对象来分配 json 值

当我执行 console.log(result);

时,我的第一个 json 对象结果如下所示

结果

{
  "computer": {
    "en": {
      "4gSSbjCFEorYXqrgDIP2FA": {
        "gallery": "Some value here",
        "dummykey": "some dummy value here"
      },
      "ksjdfsk123233165":{
        "gallery": "test value",
        "dummykey": "checking the new file here"
      }
    }
  },
  "testing": {
    "hi": {
      "2pOUGnI1oRD7nsrYs600HA": {
        "access": "not available",
        "final": "still in progress"
      }
    },
    "ja": {
      "2pOUGnI1oRD7nsrYs600HA": {
        "mediaaccess": "system check",
        "access": "available",
        "final": "done"
      },
      "sdjksd9120812nmdns9": {
        "access": "available",
        "final": "progress"
      }
    }
  },
  "hardware": {
    "us": {
      "66rzYr2BpWL1VTBHdLTdSW": {
        "fieldtest": null,
        "media": "not available"
      },
      "sjkjsp3253m899as0": {
        "fieldtest": null,
        "media": "not available"
      }
    },
    "eu": {
      "66rzYr2BpWL1VTBHdLTdSW": {
        "fieldtest": null,
        "media": "available"
      },
      "sjkjsp3253m899as0": {
        "fieldtest": "in test we are planning",
        "media": "not available"
      }
    }
  },
  "software": {
    "uk": {
      "2eAmIIuG4xkLvatkU3RUSy": {
        "testfield": "its working",
        "assign": "not yet"
      },
      "wehjbe8230291929umsnd":{
        "testfield": "working",
        "assign": "new trainee"
      }
    }
  },
  "demo": {
    "ga": {
      "4zu7f2YP4cXqR6aeeMxEt8": {
        "newkey": "hello",
        "assign": "to new assignnie"
      },
      "ksdjfns4575634n":{
        "newkey": "working",
        "assign": "new trainee"
      }
    },
  }
};

我试图比较和赋值的第二个 json 对象在下面

比较对象

{
  "computer": { "compare": "gallery" },
  "testing": { "compare": "mediaaccess" },
  "hardware": { "compare": "fieldtest" },
  "software": { "compare": "testfield" },
  "demo": { "compare": "assign" }
}

我正在尝试使用 result obj 中存在的键检查并分配存在于 compareObj 对象中的值,并在 compareValue

中分配值
let compareValue;

但问题是在分配值时我想检查 compareObj 值是否存在于 result obj 中

此处为第一个示例结果

"computer": {
    "en": {
      "4gSSbjCFEorYXqrgDIP2FA": {
        "gallery": "Some value here",
        "dummykey": "some dummy value here"
      }
    }
  },

和比较对象

 "computer": { "compare": "gallery" },

这里的 compareObj 包含 compare 键,它有 galleryresult obj 包含 gallary 作为键,所以 compareValue 应该包含 compareValue= "Some value here" 像这样

但是 在第二个例子中

 "testing": {
    "hi": {
      "2pOUGnI1oRD7nsrYs600HA": {
        "access": "not available",
        "final": "still in progress"
      }
    },
    "ja": {
      "2pOUGnI1oRD7nsrYs600HA": {
        "mediaaccess": "system check",
        "access": "available",
        "final": "done"
      }
    }
  },

compareObjObj

 "testing": { "compare": "mediaaccess" },

因为测试 hi 不包含 mediaaccess 那么它应该检查另一个对象 ja 并像这样分配值 compareValue="system check"

第三个示例结果

  "hardware": {
    "us": {
      "66rzYr2BpWL1VTBHdLTdSW": {
        "fieldtest": null,
        "media": "not available"
      }
    },
    "eu": {
      "66rzYr2BpWL1VTBHdLTdSW": {
        "fieldtest": null,
        "media": "available"
      }
    }
  },

和比较对象

"hardware": { "compare": "fieldtest" },

如果 compareObj 中存在的值在 result obj 中不可用,或者包含的值是 null 那么它应该看起来像这样 compareValue= "Untitled"

如果对象包含多个值,那么它应该从第一个对象中获取值,例如在第二个示例中 mediaaccess 不存在,因此它应该从第二语言或第三语言或第四语言中获取,如果在可用的语言中 mediaaccess 不存在然后它应该打印 Untitled

const findCompareValue = (type) => {
  const searchKey = compareObj[type]?.compare;

  const typeObject = result[type] ?? {};
  let compareValue = null;
  outer:
  for (key in typeObject) {
    const value = typeObject[key];
    for (subkey in value) {
      const subvalue = value[subkey];
      compareValue = subvalue[searchKey];
      if (compareValue != null)
        break outer;
    }
  }
  return compareValue ?? "Untitled";
};

console.log(findCompareValue("computer")); //"Some value here"
console.log(findCompareValue("fieldtest")); //"Untitled"
console.log(findCompareValue("testing")); //"system check"

你没有解释你想做什么。类型下的第一个键似乎是语言代码。如果是这种情况,从包含它的第一种语言中提取字符串值的逻辑似乎是不正确的。如果您的问题在更高层次上解释了您要实现的目标,那么答案可能对您更有用。

CodePen

let result={
  "computer": {
    "en": {
      "4gSSbjCFEorYXqrgDIP2FA": {
        "gallery": "Some value here",
        "dummykey": "some dummy value here"
      },
      "ksjdfsk123233165":{
        "gallery": "test value",
        "dummykey": "checking the new file here"
      }
    }
  },
  "testing": {
    "hi": {
      "2pOUGnI1oRD7nsrYs600HA": {
        "access": "not available",
        "final": "still in progress"
      }
    },
    "ja": {
      "2pOUGnI1oRD7nsrYs600HA": {
        "mediaaccess": "system check",
        "access": "available",
        "final": "done"
      },
      "sdjksd9120812nmdns9": {
        "access": "available",
        "final": "progress"
      }
    }
  },
  "hardware": {
    "us": {
      "66rzYr2BpWL1VTBHdLTdSW": {
        "fieldtest": null,
        "media": "not available"
      },
      "sjkjsp3253m899as0": {
        "fieldtest": null,
        "media": "not available"
      }
    },
    "eu": {
      "66rzYr2BpWL1VTBHdLTdSW": {
        "fieldtest": null,
        "media": "available"
      },
      "sjkjsp3253m899as0": {
        "fieldtest": "in test we are planning",
        "media": "not available"
      }
    }
  },
  "software": {
    "uk": {
      "2eAmIIuG4xkLvatkU3RUSy": {
        "testfield": "its working",
        "assign": "not yet"
      },
      "wehjbe8230291929umsnd":{
        "testfield": "working",
        "assign": "new trainee"
      }
    }
  },
  "demo": {
    "ga": {
      "4zu7f2YP4cXqR6aeeMxEt8": {
        "newkey": "hello",
        "assign": "to new assignnie"
      },
      "ksdjfns4575634n":{
        "newkey": "working",
        "assign": "new trainee"
      }
    },
  }
};

let compareObj= {
  "computer": { "compare": "gallery" },
  "testing": { "compare": "mediaaccess" },
  "hardware": { "compare": "fieldtest" },
  "software": { "compare": "testfield" },
  "demo": { "compare": "assign" }
};

Object.keys(compareObj).map((type)=>{
const searchKey = compareObj[type]?.compare;

  const typeObject = result[type] ?? {};
  let compareValue;
  outer:
  for (key in typeObject) {
    const value = typeObject[key];
    compareValue = null;
    for (subkey in value) {
      const subvalue = value[subkey];
      compareValue = subvalue[searchKey];
      if (compareValue != null)
        break
    }
    console.log(compareValue ?? "Untitled")
  }
  
})

所以我执行了你的代码,我得到的是 null 而不是 null 我可以得到 Untitled 来代替 null 吗?