gpt4 book ai didi

javascript - 检查对象中的任何数组是否包含空值

转载 作者:行者123 更新时间:2023-12-05 08:37:09 25 4
gpt4 key购买 nike

我有一个名为 newEntries 的对象,到目前为止它有 2 个条目:

(2) [{…}, {…}]
0: {name: "test", website: "", sector: "", house: ""}
1: {name: "", website: "", sector: "", house: ""}
length: 2
__proto__: Array(0)

可见,目前只填充了一个索引的一个字段。如果所有元素的所有字段都已填充,我需要返回 true,否则返回 false

我如何在 javascript 中这样做?

最佳答案

使用 .every.some 的组合对我们有用:-

const array1 = [
{name: "test", website: "fdf", sector: "fdf", house: "fdf"},
{name: "fds", website: "fsd", sector: "fds", house: "fsdf"}]

const array2 = [
{name: "test", website: "", sector: "fdf", house: "fdf"},
{name: "fds", website: "fsd", sector: "fds", house: "fsdf"}]


function checkArray(array){
return array.every(item =>
!Object.values(item).some(value => value === "")
);
}

console.log(checkArray(array1));
console.log(checkArray(array2));

关于javascript - 检查对象中的任何数组是否包含空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66559655/

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