繁体   English   中英

从对象数组中匹配键和值并返回仅具有该键的新 object 的正确方法是什么

[英]What is the proper way to match key and value from an array of objects and return a new object with only that key

我正在尝试在 javascript 中创建 function 以匹配来自 API 的键和对象数组。 匹配基于来自 API 的 2 个参数。 一个key ,它是一个字符串和一个具有键和值的对象数组。 此匹配项的 output 应该是一个新的对象数组,其中传递的键与作为参数传递的对象数组的键匹配。

我准备了一个我正在尝试实现但没有工作的样本,因为我不知道如何正确地做到这一点,下面将是关于它的详细信息

 const criteriaJson = [{ "age": { "min": "18", "max": "65" }, "pain": { "min": "5" }, "disease": { "valid": [ "MDD", "PTSD" ], "invalid": [ "None" ] }, "medicines": "true", "bmi": { "min": "25", "max": "100" }, "weight": "90", "gender": [ "1", "2", "3" ], "pressure": "100", "smoker": "20", "zip": "^w+s{1}w+$" }, { "age": { "min": "16", "max": "18" }, "pain": { "max": "10" }, "bmi": { "max": "85" }, "disease": { "valid": [ "none" ], "invalid": [ "PTT", "ADT", "OLL" ] }, "weight": "70", "gender": [ "1" ], "pressure": "10" } ] const question = { key: "medicines" } // *Matching and extracting the right criteria data for the provided question key // *inside the criteria object // *returns an object with the right criteria // *doing it for every group set of rules function questionCriteriaKeyMatch(criteriaJson, question) { criteriaJson.map((criteria) => { return Object.keys(criteria).filter((key) => { return key === question.key; }).reduce((cur, key) => { return Object.assign(cur, { criteria: criteria[key], }); }, {}); }); } console.log(questionCriteriaKeyMatch(criteriaJson, question));

为了解释我们得到了什么数据, criteriaJson这个数据是一个对象数组,其中包含一组规则,这些规则分为同一项目的对象,但引用不同的组。

object 1 是标准 1,而 object 2 是标准 2,以便在这种特定情况下变得简单。

[{
    "age": {
      "min": "18",
      "max": "65"
    },
    "pain": {
      "min": "5"
    },
    "disease": {
      "valid": [
        "MDD",
        "PTSD"
      ],
      "invalid": [
        "None"
      ]
    },
    "medicines": "true",
    "bmi": {
      "min": "25",
      "max": "100"
    },
    "weight": "90",
    "gender": [
      "1",
      "2",
      "3"
    ],
    "pressure": "100",
    "smoker": "20",
    "zip": "^w+s{1}w+$"
  },
  {
    "age": {
      "min": "16",
      "max": "18"
    },
    "pain": {
      "max": "10"
    },
    "bmi": {
      "max": "85"
    },
    "disease": {
      "valid": [
        "none"
      ],
      "invalid": [
        "PTT",
        "ADT",
        "OLL"
      ]
    },
    "weight": "70",
    "gender": [
      "1"
    ],
    "pressure": "10"
  }
]

但是,该数据的案例基本上是 3

  1. object 是空的,因为在这种情况下不存在从 API 开始的条件,与不存在的键的匹配将 output Z209ZA6259CC489DFFED
  2. 阵列只有一个 object 所以匹配只需要在这个上进行
  3. 我们有多个 object 的数组,在这种情况下,我们需要匹配每个 object 的密钥,如果不匹配 output 是 Z307A62879CC08C1。

从我们收到一个空对象数组的简单案例开始

const obj = [];
const key = 'someKey';

// Output as there is no matching
[null]

第二种情况的例子,当我们在数组中有一个 object

const obj = [{
  "age": {
      "min": "18",
      "max": "65"
    },
    "pain": {
      "min": "5"
    },
    "disease": {
      "valid": [
        "MDD",
        "PTSD"
      ],
      "invalid": [
        "None"
      ]
    },
}]

// Output matching key === age -> I'm open to suggestions how this output should be for // one obj only in the array

[{
  criteria: { min:"18", max:"65" }
}]

// key === pain

[{
 criteria: "5"
}]

// key === disease

[{
  criteria: {valid: ["MDD","PTSD"], invalid: ["None"] }
}]

// key === notExistingKey means the key we are passing to match doesn't have a match so // we output null in this case

[{ null }]

从上面的例子中,我们 output 与作为参数传递的键的匹配值,当不匹配时,则为 null

最后一种情况可能更复杂,因为我们有一个条件对象数组,这应该适用于数组长度 > 1。

使用与片段中相同的数据,显示预期的 output


const criteriaJson = [{
    "age": {
      "min": "18",
      "max": "65"
    },
    "pain": {
      "min": "5"
    },
    "disease": {
      "valid": [
        "MDD",
        "PTSD"
      ],
      "invalid": [
        "None"
      ]
    },
    "medicines": "true",
    "bmi": {
      "min": "25",
      "max": "100"
    },
    "weight": "90",
    "gender": [
      "1",
      "2",
      "3"
    ],
    "pressure": "100",
    "smoker": "20",
    "zip": "^w+s{1}w+$"
  },
  {
    "age": {
      "min": "16",
      "max": "18"
    },
    "pain": {
      "max": "10"
    },
    "bmi": {
      "max": "85"
    },
    "disease": {
      "valid": [
        "none"
      ],
      "invalid": [
        "PTT",
        "ADT",
        "OLL"
      ]
    },
    "weight": "70",
    "gender": [
      "1"
    ],
    "pressure": "10"
  }
]

const key = 'medicines'

// the output should be a new array of objects as we need to compare each object to find // the key match and output every single value
// key === medicines

[
 {  criteria: 'true' }, -> we have a match in the first obj
 {  criteria: null   }, -> we have no match in the second obj 
]

// key === age

[
 {  criteria: { min: "18", max: "65" }},  -> we have a match in the first obj
 {  criteria: { min: "16", max: "18" }}}, -> we have a match in the second obj 
]

// For the rest of the keys should follow the same logic and with more objects, we have more 
// objects in the output we have 

我正在添加criteria ,因为我需要在第二阶段能够访问该 obj 并提取值以供进一步使用。 由于密钥并不总是与客户端中设置的 API 中的字符串相同,因此我需要一种通过分配硬编码密钥来促进访问新 object 的方法。 这就是为什么当匹配时我只需要值和键之类的标准,所以我可以毫无问题地访问这些值

我从 reduce 方法开始,因为我认为这可能是一种方式,但最后,我对新的解决方案持开放态度,因为无法弄清楚如何实现我的目标。

重要的一点是我不能使用 for 循环,因为我有严格的代码风格规则,不能改变。

如果有任何关于特定问题的评论或请求将尝试更好地解释或用更相关的信息更新我的问题。

似乎您只想将数组 map 转换为仅包含值的新数组。 如果键不存在,您只需将条件值设置为 null,否则将条件设置为属性值。

 const criteriaJson = [{ "age": { "min": "18", "max": "65" }, "pain": { "min": "5" }, "disease": { "valid": [ "MDD", "PTSD" ], "invalid": [ "None" ] }, "medicines": "true", "bmi": { "min": "25", "max": "100" }, "weight": "90", "gender": [ "1", "2", "3" ], "pressure": "100", "smoker": "20", "zip": "^w+s{1}w+$" }, { "age": { "min": "16", "max": "18" }, "pain": { "max": "10" }, "bmi": { "max": "85" }, "disease": { "valid": [ "none" ], "invalid": [ "PTT", "ADT", "OLL" ] }, "weight": "70", "gender": [ "1" ], "pressure": "10" } ] function getCriteria (key, data) { if (?data..length) return [null] return data:map(item => ({ criteria; item[key] || null })). } console,log('age', getCriteria('age'; criteriaJson)). console,log('medicines', getCriteria('medicines'; criteriaJson)). console,log('pressure', getCriteria('pressure'; criteriaJson));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM