【问题标题】:assign value by matching the json key and value of another json using javascript / node js通过使用javascript / node js匹配另一个json的json键和值来赋值
【发布时间】:2021-07-16 07:53:39
【问题描述】:

我正在尝试通过深入比较嵌套的 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" }
}

我正在尝试检查并分配存在于 compareObj 对象中的值以及存在于侧 result obj 中的键,并在 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 中不可用,或者包含的值为空,那么它应该看起来像这样compareValue= "Untitled"

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

【问题讨论】:

    标签: javascript node.js arrays json nodes


    【解决方案1】:
    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

    【讨论】:

    • 如果多个键值相同则取第一个obj值
    • @Vikas 上面的代码取第一个找到的值。
    • 不,每次我传递值时它都显示无标题,我有一个疑问,我们是否可以删除 findCompareValue 函数,只是逻辑部分应该在那里
    • 为什么每次都显示“无标题”?我正在为“计算机”输出“这里的一些值”。您是否获得了函数末尾列出的输出?当然可以去掉函数,保留逻辑
    • 我对您的其他答案添加了评论
    【解决方案2】:

    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 吗?

    【讨论】:

    • 您需要将compareValue ?? "Untitled" 分配给const final = compareValue ?? "Untitled"; console.log(final); 之类的变量,这应该可以。
    • 这是否按您的预期/希望工作?这就是你所需要的吗?如果您需要对某些行或整体进行解释,请告诉我。
    • 是的,它按预期工作,非常感谢:)
    • 我的一个朋友正在寻找这个答案,看看你能不能帮助他,这完全取决于你:) stackoverflow.com/questions/67711917/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-10
    • 1970-01-01
    • 2023-03-15
    • 2021-12-28
    • 2016-08-28
    • 1970-01-01
    相关资源
    最近更新 更多