stringify数组中的对象,但不是数组本身

时间:2016-06-02 18:29:09

标签: javascript reactjs

我正在映射我的数组(有很多)。因此,我们的想法是将数组中的所有对象/所有内容字符串化。

  const studentInfoObj = students.map(value => (
      // for each student return json string of the objects
      // how can I just stringify everything within the array?
      // but not the array itself. Just all the objects inside?
      return JSON.stringify(???);
  ));

这是现在的json

{"Michael00043":
     // stringify from this point
     [
          {"student_num":"20293290000HJ93",
           "campus":"McHale",
           "major":"PSI"
          },
     ],
      ... more objects
     [
          {"medical_doc":"st.laurance hos",
           "emergency_contact":"Adam Harley",
           "blood_type":"O"
          },
          {"hospital":"st.laurance hos",
           "ward":"B",
          },
     ]
...

1 个答案:

答案 0 :(得分:3)

这是你的意思吗?

var students = {
  "Michael00043": [{
    "student_num": "20293290000HJ93",
    "campus": "McHale",
    "major": "PSI"
  }, {
    "medical_doc": "st.laurance hos",
    "emergency_contact": "Adam Harley",
    "blood_type": "O"
  }]


};

// loop through each student
for (student in students) {
  // stringy each value in the array that each student is
  students[student] = students[student].map(value => JSON.stringify(value));
}

console.log(students);