简体   繁体   English

通过与另一个对象(JavaScript)比较,有条件地向JSON对象添加新属性

[英]Add a new attribute to JSON object conditionally by comparing with another object (JavaScript)

For example I have two arrays "StudentInfo" 例如,我有两个数组“ StudentInfo”

[
  {
   "id": "1234",
   "name": "A"
  },
  {
   "id": "1134",
   "name": "B"
  },
  {
   "id": "2234",
   "name": "C"
  },
  {
   "id": "3234",
   "name": "D"
  }     
]

and "GoodStudentList" 和“ GoodStudentList”

[
  "1234",
  "1134"
]

So how could I add one more attribute about type of Student to StudentInfo depends on the GoodStudentList, if they have their ID in good list then their type is good, otherwise their type is Normal: 因此,我该如何在StudentInfo上添加一个有关Student类型的属性,取决于GoodStudentList,如果他们的ID在良好列表中,那么它们的类型就很好,否则它们的类型是Normal:

 [
      {
       "id": "1234",
       "name": "A",
       "type": "Good"
      },
      {
       "id": "1134",
       "name": "B"
       "type": "Good"
      },
      {
       "id": "2234",
       "name": "C"
       "type": "Normal"
      },
      {
       "id": "3234",
       "name": "D",
       "type": "Normal"
      }     
]

Sorry I know this maybe easy but I really can't understand about mapping thing in JS qwq 对不起,我知道这可能很简单,但是我真的不了解JS qwq中的映射内容

You can use forEach() to loop through the students array and test whether each id is in the good list with includes() . 您可以使用forEach()遍历students数组,并使用includes()测试每个id是否在良好列表中。 Then add the correct property depending on the result as you loop through students: 然后在遍历学生时根据结果添加正确的属性:

 let StudentInfo = [{"id": "1234","name": "A"},{"id": "1134","name": "B"},{"id": "2234","name": "C"},{"id": "3234","name": "D"} ] let GoodStudentList = ["1234","1134"] StudentInfo.forEach(student => { student.type = GoodStudentList.includes(student.id) ? "Good" : "Normal" }) console.log(StudentInfo) 

You could use the map method on the StudentInfo array, and for each element, check if that object's id exists in the GoodStudentList . 您可以在StudentInfo数组上使用map方法,对于每个元素,检查该对象的ID是否存在于GoodStudentList It could look something like this: 它可能看起来像这样:

StudentInfo = StudentInfo.map((student) => {
  if (GoodStudentList.indexOf(student.id) >= 0) {
   student.type = 'Good';
  } else {
    student.type = 'Normal';
  }
}) 

Here, you're checking if the index of the student id is greater than 0 for each student (meaning that it exists in the array). 在这里,您正在检查每个学生的学生ID的索引是否大于0(意味着它存在于数组中)。 If if it doesn't exist in the array, it will return -1 and thus not pass the condition. 如果它在数组中不存在,它将返回-1,因此不通过条件。 Returned is the StudentInfo array with all students modified to now include the type property. 返回的是StudentInfo数组,所有学生已修改为现在包括type属性。

Using plain old iterations: 使用简单的旧迭代:

 let list = [ { "id": "1234", "name": "A" }, { "id": "1134", "name": "B" }, { "id": "2234", "name": "C" }, { "id": "3234", "name": "D" } ]; let good = [ "1234", "1134" ]; for (let key in list){ for (let i of good){ if (i === list[key].id){ list[key].type = "Good"; break; } else list[key].type = "Neutral"; } } console.log(list); 

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

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