2

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?

1
  • It asks about a single string, and this is about an object array with sting values. Quite different. Commented Mar 10, 2021 at 6:56

7 Answers 7

4

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

Sign up to request clarification or add additional context in comments.

9 Comments

Actually, this is very much the same as the two .every's you've had before the edit. Both are correct. (There are no such value, that is an empty string = All values are not equal to an empty string)
@FZs in the some bit, I think I would skip iterating through rest of the object values as soon as value==="" making this approach better ? Or am I wrong in assuming that ?
So would it be with the .every. .some is really just an inverted .every...
So even if a .some condition matches, the subsequent callbacks are still performed nevertheless ?
Using some indeeed makes the operation way faster (especially if the object gets bigger) I've created a jsbench so everbody can try out himself: jsbench.me/4xkm33t6pw/1
|
4

Using .some:

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

Comments

1

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

2 Comments

That would work but that's what some/every function were introduced for
yeah, that every function is neat
1

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

Comments

0

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

1 Comment

Actually, no, this isn't the best way. It doesn't even work if only the half of an object's properties are empty.
0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.