0

I have object array obj1, obj2,

if the name of object cannot have All key name return true

if the name of object can have only one All key name and other object return false

if the name of object can have only one All key name and no other object return true

if the name of object has too many key name All return false

based on above conditions how to do in javascript.

function checkObj(ob){
  var result = ob.filter(e=>e.name=="All");
  if(result.length !== 1){
    return false;
  }
  else{
    return true;
  }
}

var  obj1=[
  {id:1, name: "All", value:"all"},
  {id:2, name: "Sun", value:"sun"},
  {id:3, name: "Mon", value:"mon"},
]

var  obj2=[
  {id:4, name: "Thur", value:"thur"},
  {id:8, name: "Mon", value:"mon"},
]

var  obj3=[
  {id:1, name: "Thur", value:"thur"},
  {id:5, name: "All", value:"all"},
  {id:2, name: "Mon", value:"mon"},
  {id:6, name: "All", value:"all"}
]

var obj4=[
{id:1, name: "All", value:"all"}
]

var r1= this.checkObj(obj1);
var r2=this.checkObj(obj2);
var r3=this.checkObj(obj3);
var r4=this.checkObj(obj4);

Expected Output:

false
true
false
true
3
  • Change if(result.length !== 1) to if(result.length > 1) Commented Oct 19, 2020 at 7:20
  • 1
    also, I would use .some() for this case developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Oct 19, 2020 at 7:20
  • @MuhammadUsman I didn't see your comment before posting my answer Commented Oct 19, 2020 at 7:35

2 Answers 2

2

You can check if the array has any objects with name = "All".

Then, return true if

  • The array has name = "All" object and the array's length is 1
  • or the array doesn't have name = "All"
function checkObj(arr) {
  const hasAll = arr.some(o => o.name === "All")
  return (arr.length === 1 && hasAll) || !hasAll
}

Here's a snippet:

function checkObj(arr) {
  const hasAll = arr.some(o => o.name === "All")
  return (arr.length === 1 && hasAll) || !hasAll
}

const obj1=[{id:1,name:"All",value:"all"},{id:2,name:"Sun",value:"sun"},{id:3,name:"Mon",value:"mon"},],
    obj2=[{id:4,name:"Thur",value:"thur"},{id:8,name:"Mon",value:"mon"},],
    obj3=[{id:1,name:"Thur",value:"thur"},{id:5,name:"All",value:"all"},{id:2,name:"Mon",value:"mon"},{id:6,name:"All",value:"all"}],
    obj4=[{id:1,name:"All",value:"all"}];

console.log( checkObj(obj1) )
console.log( checkObj(obj2) )
console.log( checkObj(obj3) )
console.log( checkObj(obj4) )

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

3 Comments

if only one All present and has other objects return false, how to check this, thanks
@codecat What is the difference between point 2 and 3 in your question? "if the name of object can have only one All key name and other object return false. if the name of object can have only one All key name and no other object return true". I did not understand the difference
thansk for reply, have added the obj4 example for "if the name of object can have only one All key name and no other object return true" obj1 example for ". if the name of object can have only one All key name and no other object return true"
2

You could take Array#every with a short circuit on counts over one.

function checkArray(array) {
    return array.every((count => ({ name }) => (count += name === "All") < 2)(0));
}

var array1 = [{ id: 1, name: "All", value: "all" }, {  id: 2, name: "Sun", value: "sun" }, { id: 3, name: "Mon", value: "mon" }],
    array2 = [{ id: 4, name: "Thur", value: "thur" }, { id: 8,  name: "Mon", value: "mon" }],
    array3 = [{ id: 1, name: "Thur", value: "thur" }, { id: 5, name: "All",  value: "all" }, { id: 2, name: "Mon",  value: "mon" }, { id: 6, name: "All", value: "all" }];

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

2 Comments

if only one All present and has other objects return false, how to check this, thanks
coun need the count variable outside of the function: let count = 0; array.every(({ name }) => (count += name === "All") < 2); return count === 1;

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.