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
7 Answers
Reset to default 4Using 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)}