如何在节点js中对两个对象进行条件检查?

How to make a condition check between two objects generic in node js?

提问人:Renee 提问时间:3/20/2023 最后编辑:BerciRenee 更新时间:3/20/2023 访问量:62

问:

在下面的代码中,我尝试计算每个主题的总分并将它们存储在标记对象中。我正在检查 edu 中的每个 obj 是否是基于主题名称(名称)和主题级别(level)的信息的一部分。如果它匹配,我将所有内容放在一个数组中。

在这里,在函数“func”中,每次将主题添加到信息中时,我都必须添加另一个“else if”进行检查。我不想总是添加此条件检查。

我想让它通用,比如说,我的信息变成:

let info = {"math": 1, "science": 2}
or
let info = {"math": 1, "science": 2, "history": 1, arts: 1}

我不想删除或添加条件检查。我希望在没有依赖关系的情况下完成响应(edu)和信息之间的检查。只有根据信息才能进行检查。

这是在节点js中。我该怎么做?我虽然是开关,但这与“如果其他”相同。

let res = {
  edu: [
    {
      name: "math",
      level: 1,
      marks: 50,
      part: 1,
    },
    {
      name: "math",
      level: 1,
      marks: 57,
      part: 2,
    },
    {
      name: "science",
      level: 2,
      marks: 70,
      part: 1,
    },
  ],
  age: 20,
  name: "abc",
}

let info = { math: 1, science: 2, history: 1 }
const func = () => {
  var marks = {}
  let subject = Object.keys(info)
  subject.forEach((subjName, index) => {
    marks[subjName] = 0
  })
  console.log("marks = ", marks)
  var consolidatedInfo = []

  res.edu.forEach(function (subj) {
    if (subj.name === Object.keys(info)[0] && subj.level === info[Object.keys(info)[0]]) {
      marks[subj.name] += subj.marks

      const eachSubjPartInfo = {
        name: subj.name,
        level: subj.level,
        marks: subj.marks,
        part: subj.part,
      }
      consolidatedInfo.push(eachSubjPartInfo)
      console.log("marks: ", marks)
    } else if (subj.name === Object.keys(info)[1] && subj.level === info[Object.keys(info)[1]]) {
      marks[subj.name] += subj.marks

      const eachSubjPartInfo = {
        name: subj.name,
        level: subj.level,
        marks: subj.marks,
        part: subj.part,
      }
      consolidatedInfo.push(eachSubjPartInfo)
      console.log("marks: ", marks)
    } else if (subj.name === Object.keys(info)[2] && subj.level === info[Object.keys(info)[2]]) {
      marks[subj.name] += subj.marks

      const eachSubjPartInfo = {
        name: subj.name,
        level: subj.level,
        marks: subj.marks,
        part: subj.part,
      }
      consolidatedInfo.push(eachSubjPartInfo)
      console.log("marks: ", marks)
    }
  })

  console.log("consolidatedInfo = ", consolidatedInfo)
  console.log("marks = ", marks)
}

func()


JavaScript 节点 .js ReactJS 对象 比较

评论


答:

1赞 cmgchess 3/20/2023 #1

您可以在每次迭代中使用逻辑进行动态检查,并仅在满足时添加。无需转换为数组,然后检查info[name] === level

我用了数组。创建具有每个主题的初始对象 0 标记 我使用了reduceObject.fromEntries(Object.keys(info).map(k => [k,0]))

let res = {    "edu": [        {            "name": "math",            "level": 1,            "marks": 50,"part": 1        },{"name": "math",            "level": 1,            "marks": 57,"part": 2},{"name": "science",            "level": 2,            "marks": 70,"part": 1},    ],    "age": 20,    "name": "abc"}

let info = {"math": 1, "science": 2, "history": 1}

const marksAccumulator = Object.fromEntries(Object.keys(info).map(k => [k,0]))

const result = res.edu.reduce((acc,{name,level,marks,part}) => {
  if(info?.[name] === level){
    acc.marksAccumulator[name] += marks
    acc.consolidated.push({name,level,marks,part})
  }
  return acc
},{marksAccumulator,consolidated:[]})

console.log(result)

从您可以获取所需的数据,依此类推resultresult.consolidated

您也可以在迭代中不解构当前元素

let res = {    "edu": [        {            "name": "math",            "level": 1,            "marks": 50,"part": 1        },{"name": "math",            "level": 1,            "marks": 57,"part": 2},{"name": "science",            "level": 2,            "marks": 70,"part": 1},    ],    "age": 20,    "name": "abc"}

let info = {"math": 1, "science": 2, "history": 1}

const marksAccumulator = Object.fromEntries(Object.keys(info).map(k => [k,0]))

const result = res.edu.reduce((acc,curr) => {
  if(info?.[curr.name] === curr.level){
    acc.marksAccumulator[curr.name] += curr.marks
    acc.consolidated.push(curr)
  }
  return acc
},{marksAccumulator,consolidated:[]})

console.log(result)

评论

0赞 Renee 3/20/2023
花了一些时间来理解代码。我没有想过使用reduce。这个流程对我有用。只是一个问题,代码正在采用合并字段。如果合并的字段太多,那么我们必须继续添加它们?
0赞 cmgchess 3/20/2023
@Renee我在这里所做的是某种叫做对象解构的东西。当然,你不必这样做。您可以写为 where 是迭代的当前元素。我解构了它,这样我就可以直接访问字段,而不必写 as 等等。其余的和一样的。希望你明白了reduce((acc,curr)currcurr.nameif(info?.[curr.name] === curr.level)acc.consolidated.push(curr)
0赞 cmgchess 3/20/2023
@Renee我也添加了一个片段
0赞 Renee 3/21/2023
我现在明白了。我应该从第一次分享的片段中理解。我将不得不再次回到概念上。非常感谢您抽出宝贵时间解释!