const o1=[ [{ "e": "3", "a": "2", "m": "1", "u": "5" }, { "e": "2", "a": { "a": "1", "m": "1", "u": "5" } }, ], [{ "e": "3", "a": { "a": "2", "m": "1", "u": "5" } }, { "e": "2", "a": { "a": "1", "m": "1", "u": "5" } }, ], ], const o2=[ [ { "e": "3", "a": "2", "m": "1", "u": "5" }, { "e": "2", "a": "1", "m": "1", "u": "5" } ], [ { "e": "3", "a": "2", "m": "1", "u": "5" }, { "e": "2", "a": "1", "m": "1", "u": "5" } ], ] 用ES6把o1转换成o2
时间: 2023-07-08 13:48:16 浏览: 54
可以使用数组的 `map` 方法来实现转换。具体实现方法如下:
```javascript
const o2 = o1.map((subArr) => {
return subArr.map((obj) => {
if (typeof obj.a === 'object') {
return {
e: obj.e,
a: obj.a.a,
m: obj.a.m,
u: obj.a.u,
};
} else {
return obj;
}
});
});
```
首先我们使用 `map` 方法遍历 `o1` 的每个子数组,然后再使用 `map` 方法遍历每个对象。如果对象的 `a` 属性为对象类型,则进行属性转换,否则不进行转换。最后返回转换后的对象数组,即可得到 `o2`。
相关问题
const o1=[ [{ "e": "3", "a": { "a": "2", "m": "1", "u": "5" } }, { "e": "2", "a": { "a": "1", "m": "1", "u": "5" } }, ], [{ "e": "3", "a": { "a": "2", "m": "1", "u": "5" } }, { "e": "2", "a": { "a": "1", "m": "1", "u": "5" } }, ], ], const o2=[ [ { "e": "3", "a": "2", "m": "1", "u": "5" }, { "e": "2", "a": "1", "m": "1", "u": "5" } ], [ { "e": "3", "a": "2", "m": "1", "u": "5" }, { "e": "2", "a": "1", "m": "1", "u": "5" } ], ] 用ES6把o1转换成o2
可以使用 `Array.prototype.map()` 方法和对象解构来转换 `o1` 到 `o2`。下面是一个示例代码:
```
const o1=[
[
{
"e": "3",
"a": {
"a": "2",
"m": "1",
"u": "5"
}
},
{
"e": "2",
"a": {
"a": "1",
"m": "1",
"u": "5"
}
},
],
[
{
"e": "3",
"a": {
"a": "2",
"m": "1",
"u": "5"
}
},
{
"e": "2",
"a": {
"a": "1",
"m": "1",
"u": "5"
}
},
],
];
const o2 = o1.map(arr => {
return arr.map(({ e, a: { a, m, u } }) => {
return { e, a, m, u };
});
});
console.log(o2);
```
在上面的代码中,我们首先定义了 `o1` 数组。然后使用两个嵌套的 `map()` 方法遍历 `o1` 中的每个对象,并通过对象解构来获取里面的属性值。最终得到一个新的数组 `o2`,其中每个元素都包含 `e`、`a`、`m` 和 `u` 属性。
String a = {"id":"cszb1","value":"999"},{"id":"cszbzd2","value":"999"},转json
这里提供一种C++的方法将字符串转换为JSON格式:
```
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <iomanip>
#include <cstring>
#include <algorithm>
#include <iterator>
#include <map>
using namespace std;
string escape(string const& s) {
ostringstream o;
for (auto c : s) {
switch (c) {
case '"': o << "\\\""; break;
case '\\': o << "\\\\"; break;
case '\b': o << "\\b"; break;
case '\f': o << "\\f"; break;
case '\n': o << "\\n"; break;
case '\r': o << "\\r"; break;
case '\t': o << "\\t"; break;
default:
if ('\x00' <= c && c <= '\x1f') {
o << "\\u" << hex << setw(4) << setfill('0') << static_cast<int>(c);
} else {
o << c;
}
}
}
return o.str();
}
string to_json(string const& s) {
return '"' + escape(s) + '"';
}
string to_json(int n) {
return to_string(n);
}
string to_json(double d) {
ostringstream o;
o << d;
return o.str();
}
template <typename T>
string to_json(vector<T> const& v) {
ostringstream o;
o << '[';
bool first = true;
for (auto const& e : v) {
if (!first) {
o << ',';
}
first = false;
o << to_json(e);
}
o << ']';
return o.str();
}
template <typename K, typename V>
string to_json(map<K, V> const& m) {
ostringstream o;
o << '{';
bool first = true;
for (auto const& p : m) {
if (!first) {
o << ',';
}
first = false;
o << to_json(p.first) << ':' << to_json(p.second);
}
o << '}';
return o.str();
}
int main() {
string a = R"([{"id":"cszb1","value":"999"},{"id":"cszbzd2","value":"999"}])";
vector<map<string, string>> v;
size_t i = 0;
while (i < a.size()) {
size_t j = a.find_first_of("{[", i);
if (j == string::npos) {
break;
}
size_t k = a.find_first_of("}]", j);
if (k == string::npos) {
break;
}
string s = a.substr(j, k - j + 1);
map<string, string> m;
size_t l = 0;
while (l < s.size()) {
size_t p = s.find_first_of(":\"", l);
if (p == string::npos) {
break;
}
size_t q = s.find_first_of("\",}", p + 1);
if (q == string::npos) {
break;
}
string key = s.substr(l + 1, p - l - 2);
string value = s.substr(p + 2, q - p - 2);
m[key] = value;
l = q + 1;
}
v.push_back(m);
i = k + 1;
}
cout << to_json(v) << endl;
return 0;
}
```
这个程序将字符串a解析为一个vector,其中每个元素都是一个map,map的key是字符串"id"和"value",value是字符串"999"。然后,使用to_json函数将vector转换为JSON格式的字符串。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)