-2
const data = [

{
 name: "Kris",
 Tel: 000000,
 address:""
},
{
 name: "Mark",
 Tel: 111111,
 address:"USA"
},
{
 name: "Charie",
 Tel: null,
 address:""
},

]

I want to find() empty prop in this array if i has find empty or null i want to return true and break to return so i dont' know how

expect: boolean type for return

from now i use

  const checkEmtry = () => data.forEach(e => Object.values(e).find(x => (x === null) || x === ''  ) )
Spectric
  • 30,714
  • 6
  • 20
  • 43
devAndDev
  • 57
  • 1
  • 5
  • 2
    What do you expect the function to return? – evolutionxbox Jul 25 '21 at 15:16
  • 1
    Please clarify the question... What is the desired output ? Object index in array ? Do you want to find all or just the first object ? – nip Jul 25 '21 at 15:17
  • expect: boolean type for return – devAndDev Jul 25 '21 at 15:18
  • What do you want `true` and `false` to mean? – T.J. Crowder Jul 25 '21 at 15:20
  • `forEach` will always return `undefined`. Consider using `every` or `some`, whichever is more appropriate? – evolutionxbox Jul 25 '21 at 15:20
  • to go a bit further from evolutionxbox's comment, it may be that you want to use `map` if you want every result. Anyway, this question should be clarified _a lot_ IMHO. For instance, you could give output expected data – Ulysse BN Jul 25 '21 at 15:29
  • Does this answer your question? [How to determine if Javascript array contains an object with an attribute that equals a given value?](https://stackoverflow.com/questions/8217419/how-to-determine-if-javascript-array-contains-an-object-with-an-attribute-that-e) – pilchard Jul 25 '21 at 15:48

2 Answers2

4

I think you're looking for the some method (instead of forEach and find), which calls a callback for each element in the array and stops the first time the callback returns true.

const checkEmtry = () => data.some(e => Object.values(e).some(x => x === null || x === ""));
// −−−−−−−−−−−−−−−−−−−−−−−−−−−^^^^−−−−−−−−−−−−−−−−−−−−−−−^^^^

Live Example:

const data = [
    {
        name: "Kris",
        Tel: 000000,
        address:""
    },
    {
        name: "Mark",
        Tel: 111111,
        address:"USA"
    },
    {
        name: "Charie",
        Tel: null,
        address:""
    },
];

const checkEmtry = () => data.some(e => Object.values(e).some(x => x === null || x === ""));

console.log(checkEmtry()); // true

To make the function more generally useful, you might accept the array to check as a parameter:

const checkEmtry = (data) => data.some(e => Object.values(e).some(x => x === null || x === ""));
// −−−−−−−−−−−−−−−−−^^^^

Side note: I wasn't sure whether you meant checkEntry or checkEmpty so I left it checkEmtry above, but you probably want to change the m or the t.

Spectric
  • 30,714
  • 6
  • 20
  • 43
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

const data = [

{
 name: "Kris",
 Tel: 000000,
 address:""
},
{
 name: "Mark",
 Tel: 111111,
 address:"USA"
},
{
 name: "Charie",
 Tel: null,
 address:""
},

]

function deepChecker(data) {
    let checkFilter = data.filter((d) => {
        return Object.values(d).filter((f) => { return f != "" && f != null }).length == Object.values(d).length
    })

    return data.length == checkFilter.length
}
console.log(deepChecker(data))
muhammed ikinci
  • 667
  • 2
  • 6
  • 18