gpt4 book ai didi

javascript - 尝试过滤 JSON

转载 作者:行者123 更新时间:2023-11-30 13:46:34 24 4
gpt4 key购买 nike

我正在尝试使用 JSON.filter() 过滤 JSON,但我的代码是错误的,我尝试了一些变体,但都失败了。有人可以告诉我我做错了什么吗?

;(async function () {
const result = await transform(xml, template);
const prettyStr = await prettyPrint(xml, { indentSize: 4});
const filterActive = JSON.filter(result, products.active === 'true');
fs.writeFileSync('./temp/converted.json', JSON.stringify(result));
});

这是没有过滤器时输出的 JSON:

{
"products": [
{
"id": "60000000425",
"sku": "0135363004",
"active": "false",
"brand": "BROWNING",
"description": "725 HUNTER 12G 28\" M/C",
"price": "1719.0000",
"category": "1",
"department": "1"
},
{
"id": "60000000000",
"sku": "00100",
"active": "true",
"brand": "",
"description": "ELEY 22 TENEX",
"price": "0.2600",
"category": "8",
"department": "3"
},

最佳答案

你需要做的:

const filterActive = result.products.filter(({active}) => active === 'true');

var result = {
"products": [{
"id": "60000000425",
"sku": "0135363004",
"active": "false",
"brand": "BROWNING",
"description": "725 HUNTER 12G 28\" M/C",
"price": "1719.0000",
"category": "1",
"department": "1"
},
{
"id": "60000000000",
"sku": "00100",
"active": "true",
"brand": "",
"description": "ELEY 22 TENEX",
"price": "0.2600",
"category": "8",
"department": "3"
}
]
};

const filterActive = result.products.filter(({active}) => active === 'true');

console.log(filterActive);

关于javascript - 尝试过滤 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59211663/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com