1

I'm working to keep validation to the incoming json message mentioned below.

"fields_group": [{
    "index": 1,
    "value": "test"
}, {
    "index": 2,
    "value": "test"
}, {
    "index": 3,
    "value": "test"
}, {
    "index": 4,
    "value": "test"
}, {
    "index": 5,
    "value": "test"
}]

Validations: 1) Index value should not be duplicate 2) Should allow indexes 1 to 5 only. 3) Make sure index exist for each value.

Can someone help me with Javascript that does the above in an optimal way? I tried with 2 for loops which is O(n2), but I need a faster solution.

1
  • Please share your current code Commented Jul 11, 2016 at 20:20

3 Answers 3

3

You can use every() and add object as optional parameter to check for duplicate index values.

var obj = {"fields_group":[{"index":1,"value":"test"},{"index":2,"value":"test"},{"index":3,"value":"test"},{"index":4,"value":"test"},{"index":5,"value":"test"}]}

var result = obj.fields_group.every(function(e) {	
  if(!this[e.index] && e.index <= 5 && e.index > 0 && e.index) {
    this[e.index] = true;
    return true;
  }
}, {});

console.log(result)

You can also use regular expression /^[1-5]$/ to check index values.

var obj = {"fields_group":[{"index":1,"value":"test"},{"index":2,"value":"test"},{"index":3,"value":"test"},{"index":4,"value":"test"},{"index":5,"value":"test"}]}

var result = obj.fields_group.every(function(e) {	
  if(!this[e.index] && /^[1-5]$/.exec(e.index)) {
    this[e.index] = true;
    return true;
  }
}, {});

console.log(result)

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

1 Comment

Thank you for the prompt response !!
0

Use the following approach with Array.map, Array.some and RegExp.test functions :

var obj = {"fields_group":[{"index":1,"value":"test"},{"index":2,"value":"test"},{"index":3,"value":"test"},{"index":4,"value":"test"},{"index":5,"value":"test"}]}
var isValid = function(obj){
    var indexes = obj.map(function(v){ return v.index; });
    return !indexes.some(function(v, k, a){ return a.lastIndexOf(v) !== k; }) 
            && indexes.length === indexes.map(Boolean).length 
            && /^[1-5]+$/.test(indexes.join(""));
}

console.log(isValid(obj.fields_group));  // true
  • !indexes.some(function(v, k, a){ return a.lastIndexOf(v) !== k; }) - ensures that all indexes are unique
  • indexes.length === indexes.map(Boolean).length - ensures that each index value exists(not empty)
  • /^[1-5]+$/.test(indexes.join("") - ensures that there's should be indexes in range from 1 to 5 only

Comments

0

Another method:

function validate(fields_group) {
  if (fields_group.length > 5) {
    console.log("The array has more than 5 elements. The max is 5.");
    return false;
  }

  var idxs = {};
  for (var i = 0; i < fields_group.length; i++) {
    var obj = fields_group[i];
    if (obj.index == null || idxs[obj.index] || obj.index < 1 || obj.index > 5) {
      console.log("An object does not have a valid index.");
      return false;
    } else {
      idxs[obj.index] = true;
    }
  }
  console.log("The feilds group is valid.");
  return true;
}

I have measured the execution time (using performace.now() on Chrome) for the answers listed, and found this to be the fastest.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.