javascript - Check if any arrays in object contain empty values - Stack Overflow

I have an object called newEntries which has 2 entries so far:(2) [{…}, {…}]0: {name: "test"

I have an object called newEntries which has 2 entries so far:

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

As it is visible, currently only one field of one index is filled. I need to return true if all the fields for all elements are filled and false otherwise.

How can I do so in javascript?

I have an object called newEntries which has 2 entries so far:

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

As it is visible, currently only one field of one index is filled. I need to return true if all the fields for all elements are filled and false otherwise.

How can I do so in javascript?

Share asked Mar 10, 2021 at 6:46 hello-worldhello-world 517 bronze badges 1
  • It asks about a single string, and this is about an object array with sting values. Quite different. – FZs Commented Mar 10, 2021 at 6:56
Add a ment  | 

7 Answers 7

Reset to default 4

Using bo of .every and .some will work for us :-

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));

Using .some:

const filled = !(arr.some(obj => Object.values(obj).some(v => !v)))

Just loop thru' all array items and all fields with a flag to check:

var newEntries = [
    {name: "test", website: "", sector: "", house: ""},
    {name: "",     website: "", sector: "", house: ""}
];

for (let i=0; i<newEntries.length; i++){
    let entry     = newEntries[i];
    let allFilled = true;

    for (let field in entry)
        if (entry[field]==""){
            allFilled = false;
            break;
        }

    if (allFilled)
        console.log(entry);
}

Loop over the array, and then for each object within it, loop over its properties. If they're empty, return false. Otherwise, it'll get to the end and return true. :)

function checkAll() {
    for (let entry of newEntries) {
        for (let property in entry)
            if(entry[property] === "") return false;
    }
    return true;
}

hi this is the best way:

var newEntries = [
    {name: "test", website: "", sector: "", house: ""},
    {name: "",     website: "", sector: "", house: ""}
];

let result = true;

for (let i=0; i<newEntries.length; i++){
    let data = newEntries[i];
    let t = Object.values(data).some(val => val);
    if (!t) {
        result = false;
    }
}

console.log(result);

Loop through all the items

        var newEntries = [
                {name: "test", website: "", sector: "", house: ""},
                {name: "",     website: "", sector: "", house: ""}
            ];
            newEntries.forEach(myFunction);
            function myFunction(item) {
            if (array === undefined || array.length == 0) {
              return false;
            }
            else{
            return true;
            }
            }

var arr = [{a:1,b:"",c:"bhj"},{a:1,b:"hgh",c:"bhj"}]
var result = arr.filter(a => a.a != "" && a.b != ""&& a.c != "");
if(result.length===arr.length){
  console.log(true)
}else{
 console.log(false)}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744884174a4599020.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信