Javascript 这个名字能让人不那么困惑。)它确实对我有用。原始对象已解析元属性。不过,请注意,for in并不是在数组中循环的最佳选择。更多信息,请参阅。相反:for(const entry of goodsIssued){entry.meta=JSON.parse(e
Javascript 这个名字能让人不那么困惑。)它确实对我有用。原始对象已解析元属性。不过,请注意,for in并不是在数组中循环的最佳选择。更多信息,请参阅。相反:for(const entry of goodsIssued){entry.meta=JSON.parse(e,javascript,node.js,typescript,Javascript,Node.js,Typescript,这个名字能让人不那么困惑。)它确实对我有用。原始对象已解析元属性。不过,请注意,for in并不是在数组中循环的最佳选择。更多信息,请参阅。相反:for(const entry of goodsIssued){entry.meta=JSON.parse(entry.meta);}你帮我解决了这个问题,我更新了这个问题,为什么你不把meta存储为一个对象而不是sring?事实上,等等,更新确实改变了一些东西。:-)你的对象不再是一个数组,而是一个只有一个属性的对象,GoodSisued,那就是数组
这个名字能让人不那么困惑。)它确实对我有用。原始对象已解析元属性。不过,请注意,
for in
并不是在数组中循环的最佳选择。更多信息,请参阅。相反:for(const entry of goodsIssued){entry.meta=JSON.parse(entry.meta);}
你帮我解决了这个问题,我更新了这个问题,为什么你不把meta存储为一个对象而不是sring?事实上,等等,更新确实改变了一些东西。:-)你的对象不再是一个数组,而是一个只有一个属性的对象,GoodSisued,那就是数组。您的代码需要使用该数组,而不是goodsIssued本身。(您可能希望更改变量名以减少混淆。)
const goodsIssued = await goods
.find({ category: username, tokenName })
.sort([['createdAt', -1]])
.limit(2)
.exec();
{
"goodsIssued": [
{
"minted": false,
"_id": "5e3163597fd0ad2113bcdefe",
"category": "gameco11",
"goodId": 64,
"issuedTo": "player2",
"memo": "this is a test token",
"meta": "{\"data\": \"text\", \"test\":1, \"works\": true}",
"tokenName": "token5",
"createdAt": "2020-01-29T10:50:01.257Z",
"updatedAt": "2020-01-29T10:50:01.257Z",
"__v": 0
},
{
"minted": false,
"_id": "5e3163587fd0ad2113bcdefd",
"category": "gameco11",
"goodId": 63,
"issuedTo": "player2",
"memo": "this is a test token",
"meta": "{\"data\": \"text\", \"test\":1, \"works\": true}",
"tokenName": "token5",
"createdAt": "2020-01-29T10:50:00.691Z",
"updatedAt": "2020-01-29T10:50:00.691Z",
"__v": 0
}
]
}
for (const key in goodsIssued) {
if (goodsIssued.hasOwnProperty(key)) {
const parsedMeta = JSON.parse(goodsIssued[key].meta);
goodsIssued[key].meta = parsedMeta;
}
}
// 1. Get the array, not the object containing it
let goodsIssued = (await /*...get the goods*/).goodsIssued;
// 2. Create a new, unfrozen array with objects with unfrozen properties,
// and parse `meta` along the way
goodsIssued = [...goodsIssued.map(entry => ({...entry, meta: JSON.parse(entry.meta)}))];