gpt4 book ai didi

javascript - 根据过滤值删除

转载 作者:行者123 更新时间:2023-12-04 10:53:01 24 4
gpt4 key购买 nike

我有一个带有目录和频率(目录的清理频率)的 parsed_JSON。

如果所有的子目录频率都设置为“不”,我想删除该部分。

例如:

[ { label: 'Offices',
rows: [ { freq: '1w'},{ freq: '2w'},{ freq: 'not'} ] },
{ label: 'Kitchen and community areas – Extras',
rows: [ { freq: 'not'},{ freq: 'not'},{ freq: 'not'} ]
},
]

在这种情况下,应删除标有“厨房和社区区域 - 附加功能”的部分。

我使用以下代码实现了这一点:
  const mapped_sections      = _.map(parsed_json, section => ({
label : section.label,
rows : _.map(section.rows, row => _.merge({}, default_row, row)),
}));

const sections = _.forEach(mapped_sections, (section, i) => {
let not_length_count = 0;
_.forEach(section, (rows) => {
_.forEach(rows, (row) => {
if (row.freq === "not") {
not_length_count += 1;
}
});
if (not_length_count === rows.length) {
mapped_sections.splice(i, 1);
}
});
});

但我想用 ES6 方法重构它,比如 filter()并且仅通过 mapped_sections 映射

我一直在尝试,但卡在这里:
const sections      = _.map(parsed_json, (section, i) => {
const test = ((section.rows.filter(item => item.freq === "not"))
&& (section.rows.filter(item => item.freq === "not").length === section.rows.length)
? section.rows.slice(i, 1)
: section.rows
);
return (
section.label,
_.map(test, row => _.merge({}, default_row, row))
);
});

任何帮助将非常感激。谢谢!

最佳答案

你可以运行一个不 !在带有 every 的元素行上功能如下:

const myList = [
{
label: 'Offices',
rows: [{ freq: '1w'},{ freq: '2w'},{ freq: 'not'}]
},
{
label: 'Kitchen and community areas – Extras',
rows: [{ freq: 'not'},{ freq: 'not'},{ freq: 'not'}]
},
]

const result = myList.filter(el => !el.rows.every(r => r.freq === 'not'))

console.log(result)


所有项目 freq s not被过滤掉。

关于javascript - 根据过滤值删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59374697/

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