【问题标题】:stringify objects within an array but not the array itself字符串化数组中的对象,而不是数组本身
【发布时间】:2016-06-02 18:29:09
【问题描述】:

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

  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",
          },
     ]
...

【问题讨论】:

  • 您的解决方案是正确的。你想要什么?
  • 您的 JSON 格式错误。你能举一个有效的例子吗?你有一个开始[,但没有结束]
  • @IMTheNachoMan 是的,很抱歉,它确实是。它非常长。我只是尝试发布一个简单的示例。
  • @risabhdev 好吧,我只是想弄清楚要字符串化的内容。如果我返回 JSON.stringify(value);它只是打印出“Michael00043”.. 但我正在尝试将 Michael00043/student 中的所有内容字符串化

标签: javascript reactjs


【解决方案1】:

这是你的意思吗?

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);

【讨论】:

  • 这看起来可行。我只是很难使用 jsx 语法
猜你喜欢
  • 2010-10-04
  • 1970-01-01
  • 1970-01-01
  • 2018-02-21
  • 1970-01-01
  • 2011-10-29
  • 2017-07-13
  • 2021-12-02
  • 2017-08-24
相关资源
最近更新 更多