0

I have an array of object ,wanted to check the key "PhysicalVirtual" has same value in all objects. if it has same value, should return true else it should return false.

code which I have tried:

var testArray=[{
    "Hostname": "NDCWCAP03",   
    "PhysicalVirtual": "P",
    "Tier1": null,
    "Tier3": null,    
}, {
    "Hostname": "NDCWCAP05",
    "PhysicalVirtual": "P",
    "Tier1": null,
    "Tier3": null,    
}];

gs.info(testArray.find(key => key.PhysicalVirtual === 'P'));

Error: getting syntax error here

Evaluator: com.glide.script.RhinoEcmaError: syntax error
   script : Line(13) column(30)
console.log(testArray.find(key => key.PhysicalVirtual === 'P'));

3 Answers 3

2

Use Array.every()

let testArray = [{Hostname:"NDCWCAP03",PhysicalVirtual:"P",Tier1:null,Tier3:null},{Hostname:"NDCWCAP05",PhysicalVirtual:"P",Tier1:null,Tier3:null}];

let PV1 = testArray[0].PhysicalVirtual
let allMatch = testArray.every(obj => obj.PhysicalVirtual === PV1)
console.log(allMatch)

Edit: Without Array.every()

var testArray = [{Hostname:"NDCWCAP03",PhysicalVirtual:"P",Tier1:null,Tier3:null},{Hostname:"NDCWCAP05",PhysicalVirtual:"P",Tier1:null,Tier3:null}];

var PV1 = testArray[0].PhysicalVirtual;
var allMatch = true;
for(var i=0; i<testArray.length; i++){
  if(testArray[i].PhysicalVirtual !== PV1){
    allMatch = false;
    break;
  }
}
console.log(allMatch)

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

4 Comments

am using this code servicenow widget let is not supported .getting this error : Evaluator: com.glide.script.RhinoEcmaError: let is an ECMAScript 6 feature - not supported
is there any alternative for let
I tried with var its not working.I think every is not supported in servicenow.
@krish you can do the same with a for loop and a Boolean, just a little more complicated. I don’t have time now but if I remember I will revise my answer.
1

You can use every method of arrays to test a condition in all its members like:

const isTrue = testArray.every(
  ({ PhysicalVirtual }) => PhysicalVirtual === testArray[0].PhysicalVirtual
);

console.log(isTrue)
>>> True

1 Comment

am using this code in servicenow,I think every is not supported in servicenow.can you pls let me know any alternavtive?
1

JS Array.every is best solution for this

var testArray=[{
    "Hostname": "NDCWCAP03",   
    "PhysicalVirtual": "P",
    "Tier1": null,
    "Tier3": null,    
}, {
    "Hostname": "NDCWCAP05",
    "PhysicalVirtual": "P",
    "Tier1": null,
    "Tier3": null,    
}];

//ES6
//let PhysicalVirtualValue = testArray[0]?.PhysicalVirtual;
//let hasValue = false;
//if (testArray.length)
//  hasValue = testArray.every( e=> e.PhysicalVirtual == PhysicalVirtualValue);

//console.log(hasValue);

//without Array.every
var PhysicalVirtualValue = testArray[0]? testArray[0].PhysicalVirtual : '';
var hasValue = true;
for(var i = 0; i < testArray.length; i++) {

  if (testArray[i].PhysicalVirtual != PhysicalVirtualValue){
    hasValue = false;
    break;
  }    
}


console.log(hasValue);

1 Comment

am using this code in servicenow,I think every is not supported in servicenow.can you pls let me know any alternavtive?

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.